php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #79800 Wrong documented
Submitted: 2020-07-06 10:14 UTC Modified: 2020-07-07 07:32 UTC
From: behling at mumbomedia dot de Assigned: cmb (profile)
Status: Not a bug Package: MySQLi related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: behling at mumbomedia dot de
New email:
PHP Version: OS:

 

 [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.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-07-06 14:43 UTC] cmb@php.net
-Status: Open +Status: Feedback -Package: Documentation problem +Package: MySQLi related -Assigned To: +Assigned To: cmb
 [2020-07-06 14:43 UTC] 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-07 07:06 UTC] behling at mumbomedia dot de
-Status: Feedback +Status: Assigned
 [2020-07-07 07:06 UTC] 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-07 07:23 UTC] behling at mumbomedia dot de
-Status: Assigned +Status: Closed
 [2020-07-07 07:23 UTC] 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:26 UTC] cmb@php.net
-Status: Closed +Status: Not a bug
 [2020-07-07 07:26 UTC] cmb@php.net
> So if you want to get all rows, you have to call it in a loop.

Either that, or use mysqli_fetch_all().
 [2020-07-07 07:32 UTC] behling at mumbomedia dot de
Yeah, but fetch_all only works if your server has the mysqlnd-driver.
On the system I work with this isn't the case so I have to use a loop + some of the mysqli_fetch_*-functions.

When I understand it right I can also use mysqli_fetch_assoc if I only want a associated array or mysqli_fetch_row for a numeric array.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 17:01:58 2024 UTC