|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2020-07-06 10:14 UTC] behling at mumbomedia dot de
Description: ------------ --- From manual page: https://php.net/mysqli-result.fetch-array --- mysqli_fetch_array — Fetch a result row as an associative, a numeric array, or both That's not true. It fetchs the entire result set. This is also the different between mysqli_fetch_row or mysqli_fetch_assoc. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 13:00:01 2025 UTC |
Alright here the snippet <?php $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); /* check connection */ if ($mysqli->connect_errno) { printf("Connect failed: %s\n", $mysqli->connect_error); exit(); } $query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3"; // So this could return up to 3 rows $result = $mysqli->query($query); /* numeric array */ $row = $result->fetch_array(MYSQLI_NUM); // The resulting array is something like array('Germany','DE','USA','US','Great Britain','EN'); printf ("%s (%s)\n", $row[0], $row[1]); /* associative array */ $row = $result->fetch_array(MYSQLI_ASSOC); // $row should look like this array(array('Name'=>'Germany','CountryCode'=>'DE'),array('Name'=>'USA','CountryCode'=>'US'),array('Name'=>'Great Britain','CountryCode'=>'EN')); printf ("%s (%s)\n", $row[0]["Name"], $row[0]["CountryCode"]); /* associative and numeric array */ $row = $result->fetch_array(MYSQLI_BOTH); printf ("%s (%s)\n", $row[0][0], $row[0]["CountryCode"]); /* free result set */ $result->free(); /* close connection */ $mysqli->close(); ?>My fault. The function only retrieve one row. So if you want to get all rows, you have to call it in a loop. Something like this: while($row = $result->fetch_array(MYSQLI_ASSOC)){ $row2[] = $row; }