Wrongar Wrongar - 4 years ago 212
MySQL Question

Using MySQLI to fetch a column and convert to array

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());
$sql_query = "SELECT Desired_column FROM Table_Name";
$result_array = $sql->query($sql_query);
while($row = $result_array->fetch_row()) {
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

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download