I
I
Ibishka2020-04-28 18:05:22
PHP
Ibishka, 2020-04-28 18:05:22

Why does the answer come randomly from the database?

$cartGoods = json_decode(file_get_contents('php://input'), true);
$goodsId = implode(",", array_map(
  function ($e) {
    return array_keys($e)[0];
  },
  $cartGoods
));

$goods = mysqli_query($connection, "SELECT * FROM `goods` WHERE `id` in ($goodsId)");
while ($item = mysqli_fetch_assoc($goods)) {
...
}

$goodsId == 5.6 but products come from the database in 6.5 order. Why?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ibishka, 2020-04-29
@Ibishka

mysqli_query($connection, "SELECT * FROM `goods` WHERE `id` IN ($goodsId) ORDER BY FIELD(`id`,$goodsId)

A
Anton Anton, 2020-04-28
@Fragster

The order (order by) is not defined, and it is more convenient for the DBMS server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question