Doc #79800 [Csd->Nab]: Wrong documented
Edit report at https://bugs.php.net/bug.php?id=79800&edit=1
ID: 79800
Updated by: cmb@php.net
Reported by: behling at mumbomedia dot de
Summary: Wrong documented
-Status: Closed
+Status: Not a bug
Type: Documentation Problem
Package: MySQLi related
PHP Version: Irrelevant
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
> So if you want to get all rows, you have to call it in a loop.
Either that, or use mysqli_fetch_all().
Previous Comments:
------------------------------------------------------------------------
[2020-07-07 07:23:24] behling at mumbomedia dot de
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;
}
------------------------------------------------------------------------
[2020-07-07 07:06:02] behling at mumbomedia dot de
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();
?>
------------------------------------------------------------------------
[2020-07-06 14:43:18] cmb@php.net
> It fetchs the entire result set.
It is not supposed to. Please provide a minimal self-contained
reproduce script which shows that behavior. Thanks!
------------------------------------------------------------------------
[2020-07-06 10:14:26] 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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79800&edit=1
Thread (6 messages)