-4

How can I loop this table in PHP?

$query= "SELECT * FROM table1";

$select= mysqli_query($connection, $query);

$row = mysqli_fetch_array($select);

while ($row = mysqli_fetch_array("$select")) // line 21
{
    echo $row["column1"];
}

I have updated the code and I get this error

Recoverable fatal error : Object of class mysqli_result could not be converted to string on line 21

Dharman
  • 30,962
  • 25
  • 85
  • 135
Bengi Besçeli
  • 3,638
  • 12
  • 53
  • 87

1 Answers1

2

There are multiple issues with your current code.

  1. You fetch the first row with $row = mysqli_fetch_array($select); (line 3), but you don't do anything with it. This means that the first result is discarded.
  2. Your while loop attempts to loop over an incorrect variable ($query is a string, not the result-object), and you've quoted it into a string - you need to do it as you were with the first fetch (line 3).
  3. You don't do anything inside your loop, so the results aren't printed. At the very least, you should print them with echo.
$query = "SELECT * FROM table1";
$result = mysqli_query($connection, $query);

while ($row = mysqli_fetch_array($result)) {
    echo $row["column1"]."<br />\n";
}
Qirel
  • 25,449
  • 7
  • 45
  • 62