Reputation: 982
I need to get all the rows from result object. I’m trying to build a new array that will hold all 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
is supposed to be the new array that contains all, rows but instead I get an empty array.
Any ideas why this is happening?
Upvotes: 15
Views: 92262
Reputation: 158007
Newest versions of mysqli have some improvements that can simplify such a task.
First, of all, there is a useful function to return an array with all rows returned by a query, mysqli_fetch_all()
It means in case you need a simple enumerated array, the code would be much simpler:
$query = "SELECT domain FROM services";
$result = $sql->query($query);
return $result->fetch_all(MYSQLI_ASSOC);
or even all in one line,
return $sql->query("SELECT domain FROM services")->fetch_all(MYSQLI_ASSOC);
However, if you need to use some column to index the resulting array, you still need a loop like this:
$query = "SELECT id, domain FROM services";
$result = $sql->query($query);
$data = [];
while ($row = $result->fetch_assoc()) {
$data[$row['id']] = $row;
}
Note that you should always initialize an array before filling it up, because such a variable could already exist.
Also, mysqli_result
class is now Traversable. It means you can use it in the foreach loop right away, as though it's an array contains all rows from the database:
$query = "SELECT domain FROM services";
$result = $sql->query($query);
foreach ($result as $row) {
echo $row['domain'];
}
But it is effectively just a syntax sugar for the while loop - you cannot access values of $result
"array" directly.
This question is a decade old, and the way a connection is made and the query is performed, both in the question and the accepted answer, are obsoleted and frowned upon nowadays.
When a connection is made, there are several things to keep in mind. I wrote an article on how to connect with mysqli properly that provides a correct connection example emphasizing on the following issues:
die(mysqli_connect_error())
)When it goes to running a query, there are several things to keep in mind as well:
mysqli_query()
mysqli_stmt_get_result()
should be used in order to use familiar fetch functions to get the resulting rows. In case this function is not available you are probably to tick some checkbox in your cpanel (look for one labeled mysqlnd
).die(mysqli_error())
). Thanks to the proper error mode, mysqli will report all errors automatically.Upvotes: 22
Reputation: 625447
You had a slight syntax problem, namely an errant semi-colon.
while($row = $result->fetch_row());
Notice the semi-colon at the end? It means the block following wasn't executed in a loop. Get rid of that and it should work.
Also, you may want to ask mysqli to report all problems it encountered:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$sql = new mysqli($config['host'], $config['user'], $config['pass'], $config['db_name']);
$query = "SELECT domain FROM services";
$result = $sql->query($query);
$rows = [];
while($row = $result->fetch_row()) {
$rows[] = $row;
}
return $rows;
Upvotes: 28