Doc #79800 [Nab]: Wrong documented

From: Date: Tue, 07 Jul 2020 07:32:33 +0000
Subject: Doc #79800 [Nab]: Wrong documented
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-17694@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79800&edit=1

 ID:                 79800
 User updated by:    behling at mumbomedia dot de
 Reported by:        behling at mumbomedia dot de
 Summary:            Wrong documented
 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:

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.


Previous Comments:
------------------------------------------------------------------------
[2020-07-07 07:26:20] 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: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)

« previous php.doc.bugs (#17694) next »