php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #80844 Support assoc/num/both for mysqli_result iterated arrays
Submitted: 2021-03-07 17:50 UTC Modified: 2021-03-08 16:41 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: olafvdspek at gmail dot com Assigned:
Status: Open Package: MySQLi related
PHP Version: Irrelevant OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2021-03-07 17:50 UTC] olafvdspek at gmail dot com
Description:
------------
PDO supports this, it'd be nice if mysqli itself supported this as well.

Test script:
---------------
$mysqli = new mysqli('', 'pdo', '123', 'pdo');
foreach ($mysqli->query("select uid, name from users") as [$uid, $name]) // 3
  print("$uid: $name\n");



Actual result:
--------------
// PHP Warning:  Undefined array key 0 on line 3
// PHP Warning:  Undefined array key 1 on line 3

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-03-07 23:00 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2021-03-07 23:00 UTC] requinix@php.net
Hint:

foreach ($mysqli->query("select uid, name from users") as $row)
  print_r($row);
 [2021-03-07 23:10 UTC] olafvdspek at gmail dot com
Not sure what you're trying to say, I'm aware as $row works.. 
This is a request for array destructuring to be supported too, like it is with PDO.
 [2021-03-07 23:31 UTC] requinix@php.net
And what is the value of $row? What output do you see?
 [2021-03-08 10:01 UTC] olafvdspek at gmail dot com
// mysqli
Array
(
    [uid] => 1
    [name] => Alpha
)

// PDO
Array
(
    [uid] => 1
    [0] => 1
    [name] => Alpha
    [1] => Alpha
)
 [2021-03-08 10:07 UTC] rtrtrtrtrt at dfdfdfdf dot dfd
RTFM

https://www.php.net/manual/en/mysqli-result.fetch-array.php

 resulttype

    This optional parameter is a constant indicating what type of array should be produced from the current row data. The possible values for this parameter are the constants MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH.

    By using the MYSQLI_ASSOC constant this function will behave identically to the mysqli_fetch_assoc(), while MYSQLI_NUM will behave identically to the mysqli_fetch_row() function. The final option MYSQLI_BOTH will create a single array with the attributes of both.
 [2021-03-08 13:43 UTC] requinix@php.net
As you can see, the value given through the iterator is an array that contains only associative string keys, not also numeric keys. It can be destructured - if you specify the keys.

...as ["uid" => $uid, "name" => $name]
 [2021-03-08 14:14 UTC] olafvdspek at gmail dot com
I know.. hence my feature request (to support destructuring without keys).
 [2021-03-08 15:26 UTC] requinix@php.net
Making the iterator return a different kind of array would be a breaking change for code that thinks the array is purely associative.

Is it really such a problem to destructure with keys?
 [2021-03-08 16:15 UTC] olafvdspek at gmail dot com
I like code to be as simple as possible, having to repeat the code is less simple than is possible..

The inconsistency with PDO seems weird too.
 [2021-03-08 16:18 UTC] olafvdspek at gmail dot com
If the potential incompatibility is a problem you could add a flag to the query function, indicating what the iterator should return.
 [2021-03-08 16:41 UTC] requinix@php.net
-Summary: Array destructuring for mysqli_result +Summary: Support assoc/num/both for mysqli_result iterated arrays -Status: Not a bug +Status: Open
 [2023-04-24 06:56 UTC] misadvivek at gmail dot com
Education center web are sharing latest news about education, classess, courses, stydy, learning, college education, govt job notification, etc. More info to visit:(https://educationcenterweb.com)github.com
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 14:01:31 2024 UTC