1

So I've been trying various other threads, but none seem to use MySQLI, nor work. I'm attempting to query a MySQL table via MySQLI, the convert the returned column into an array to be used later. Here's what I have so far.

$sql = new mysqli("URL", "user", "pass","db") or die ("Not connected");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit;
}
$sql_query = "SELECT Desired_column FROM Table_Name";
$result_array = $sql->query($sql_query);
while($row = $result_array->fetch_row()) {
    $rows[]=$row;
}
$result_array->close();
$sql->close();
return $rows;

And yes... I'm a complete novice at coding so PHP vets, enjoy seeing how far you've come!

The error I am getting is

Call to a member function fetch_row() on Boolean on line 60

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Wrongar
  • 23
  • 1
  • 4

1 Answers1

3

Try something like this:

$query = "SELECT Desired_column FROM Table_Name";
$results = mysqli_query($sql, $query);

$rows = [];

while($row = mysqli_fetch_assoc($results)) {
    $rows[] = $row;
}

Edit 1

  • Changed array_push($rows, $row); to $rows[] = $row; to reduce function calls
Spholt
  • 3,724
  • 1
  • 18
  • 29