php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81103 mysqli_both overwrites numerical indexes
Submitted: 2021-06-03 19:35 UTC Modified: 2021-06-14 09:13 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: dharman@php.net Assigned:
Status: Wont fix Package: MySQLi related
PHP Version: 7.4.20 OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dharman@php.net
New email:
PHP Version: OS:

 

 [2021-06-03 19:35 UTC] dharman@php.net
Description:
------------
Per the documentation

"If two or more columns of the result have the same field names, the last column will take precedence and overwrite the earlier data. In order to access multiple columns with the same name, the numerically indexed version of the row must be used."

When executing a query with numerical column names, associative names overwrite the numerical indices. For example, in the code below it is impossible to access value 'a' as it has been overwritten with 'b'

Test script:
---------------
$res = $mysqli->query("SELECT 'abc', 'a' AS `1`, 'b' AS `1`");
var_dump($res->fetch_array());

Expected result:
----------------
array(4) {
  [0]=>
  string(3) "abc"
  ["abc"]=>
  string(3) "abc"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "b"
}

Actual result:
--------------
array(4) {
  [0]=>
  string(3) "abc"
  ["abc"]=>
  string(3) "abc"
  [1]=>
  string(1) "a"
  [2]=>
  string(1) "b"
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-06-03 19:55 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2021-06-03 19:55 UTC] requinix@php.net
So don't use numerical aliases? Or don't use MYSQLI_BOTH?

What kind of solution do you have in mind?
 [2021-06-03 20:21 UTC] dharman@php.net
Sorry, I messed up the test case. 

This one should be better:

$res = $mysqli->query("SELECT 'a' AS `0`, 'b' AS `0`");

The expected result here is 

array(2) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
}

The idea is that numerical indices must take precedence over the associative keys. I don't have a solution for this.
 [2021-06-03 20:22 UTC] dharman@php.net
-Status: Feedback +Status: Open
 [2021-06-03 20:37 UTC] fffg at gaeeer dot gg
garbage in, garbage out and a query like that shouldn't exist in any codebase - and if it exists common sense says USE_BOTH makes no sense
 [2021-06-04 11:32 UTC] cmb@php.net
FTR, this is related to the fix for bug #79084.

Anyhow, given that column names may be numeric, I don't think the
mentioned part of the documentation makes sense.  I suggest to
reword that to something like:

| If two or more columns of the result have the same field names,
| the last column will take precedence and overwrite the earlier
| data. It is recommended to use the <constant>MYSQLI_NUM</constant
| <parameter>mode</parameter> for such queries.
 [2021-06-08 14:05 UTC] nikic@php.net
I'm inclined to close this as Won't Fix. Bug #79084 was a legitimate issue, but here there is no obviously correct way in which it should behave.
 [2021-06-14 09:13 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2021-06-14 09:13 UTC] nikic@php.net
Closing as won't fix per above discussion.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 17:01:32 2024 UTC