Get an array of strings with mysqli result

I am trying to check if a row matches one of the fields in a specific column of the table. To do this, I need to get all the rows from the result object. Im using php 5.2.x, therefore, cannot use the fetch_row_all method; instead, Im trying to create a new array that will contain all the rows.

Here is my code:

 $sql = new mysqli($config['host'],$config['user'],$config['pass'],$config['db_name']); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $query = "SELECT domain FROM services"; $result = $sql->query($query); while($row = $result->fetch_row()); { $rows[]=$row; } $result->close(); $sql->close(); return $rows; 

$rows should be a new array that contains all the rows, but instead I get an empty array.

Any ideas why this is happening?

+4
source share
1 answer

You had a slight syntax problem, namely the erroneous semicolon.

 while($row = $result->fetch_row()); 

Pay attention to the comma at the end? This means that the next cycle did not run in the cycle. Get rid of it, and it should work.

In addition, you can check if the query really works:

 $sql = new mysqli($config['host'], $config['user'], $config['pass'], $config['db_name']); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit; } $query = "SELECT domain FROM services"; $result = $sql->query($query); if (!$result) { printf("Query failed: %s\n", $mysqli->error); exit; } while($row = $result->fetch_row()) { $rows[]=$row; } $result->close(); $sql->close(); return $rows; 
+19
source

All Articles