0

I am trying to fetch the values of a column in PHP and I am not sure why it is fetched at random. In the table, they are ordered from 1 to 5 and when I echo I get 14253. I have a similar function for another column and it works great, same as this one. Did I do something wrong?

public function getSKUProducts() {
        $sql = "SELECT SKU FROM Product_table";
        $stmt = $this->connect()->query($sql);
        $array = Array();
        //get data from db
        while($row = $stmt->fetch_assoc()) {
            echo $row['SKU'];
            $array[] =  $row['SKU'];  
        }
        return $array;
    }

Stephano.
  • 3
  • 2
  • 2
    Use `Order By COLUMN_NAME` in your query. – Hackinet Jun 07 '21 at 15:33
  • 2
    That's just SQL behaviour. There is no inherent ordering, you have to use explicit ordering if you want to ensure you will get them in the same order every time. – El_Vanja Jun 07 '21 at 15:34
  • @Hackinet I feel so dumb, thanks a lot. Do you know why is it happening just for this column tho? – Stephano. Jun 07 '21 at 15:36

0 Answers0