Edit report at https://bugs.php.net/bug.php?id=79084&edit=1
ID: 79084
User updated by: rob-phpbugs at tigertech dot com
Reported by: rob-phpbugs at tigertech dot com
Summary: mysqli fetch_array returns different results on PHP
7.3+ with some column names
Status: Open
Type: Bug
Package: MySQLi related
Operating System: Linux
PHP Version: 7.3.13
Block user comment: N
Private report: N
New Comment:
I'm not sure I understand what you said, for which I apologize.
Are you saying that when you run the test script on both PHP 7.2 and 7.3, it prints the same
results? Could you post what the output of the script is in both cases? Here's the output of a
test I just did:
$ cat test.php
<?php
$mysqli = mysqli_connect("localhost", "test", "[redacted]",
"test");
$sql = "SELECT 0 as 2007, 0 as 2008, 0 as 2020";
$res = $mysqli->query($sql);
$row = $res->fetch_array();
print "PHP version: " . phpversion() . "\n";
print_r($row);
$ php-7.2 test.php
PHP version: 7.2.26
Array
(
[0] => 0
[2007] => 0
[1] => 0
[2008] => 0
[2] => 0
[2020] => 0
)
$ php-7.3 test.php
PHP version: 7.3.13
Array
(
[0] => 0
[2007] => 0
[2008] => 0
[2009] => 0
[2020] => 0
)
Previous Comments:
------------------------------------------------------------------------
[2020-01-09 08:54:20] cmb@php.net
What happens should be similar to
<?php
$a = [];
$a['2007'] = 0;
$a[] = 0;
?>
So I'm surprised that you're reporting that would have been
different with PHP 7.2. I cannot reproduce that.
------------------------------------------------------------------------
[2020-01-08 19:30:15] rob-phpbugs at tigertech dot com
I realize I included a confusing double "SELECT" in the example script because of how I
was narrowing down the bug originally. That's not necessary and unrelated; the same thing
happens with the simpler query of:
<?php
$mysqli = mysqli_connect("localhost", "user", "password",
"db");
$sql = "SELECT 0 as 2007, 0 as 2008, 0 as 2020";
$res = $mysqli->query($sql);
$row = $res->fetch_array();
echo print_r($row, 1);
------------------------------------------------------------------------
[2020-01-08 19:03:37] rob-phpbugs at tigertech dot com
Description:
------------
If a query has column names that are all numeric, like this:
SELECT * FROM (SELECT 0 as 2007, 0 as 2008, 0 as 2020) AS x;
And you use "fetch_array()" on it, PHP 7.2 gives this result:
Array
(
[0] => 0
[2007] => 0
[1] => 0
[2008] => 0
[2] => 0
[2020] => 0
)
But PHP 7.3 and 7.4 give this result:
Array
(
[0] => 0
[2007] => 0
[2008] => 0
[2009] => 0
[2020] => 0
)
The latter is wrong: it should not contain "2009", and it is missing "[1]" and
"[2]".
Test script:
---------------
<?php
$mysqli = mysqli_connect("localhost", "user", "password",
"db");
$sql = "SELECT * FROM (SELECT 0 as 2007, 0 as 2008, 0 as
2020) AS x";
$res = $mysqli->query($sql);
$row = $res->fetch_array();
echo print_r($row, 1);
Expected result:
----------------
Array
(
[0] => 0
[2007] => 0
[1] => 0
[2008] => 0
[2] => 0
[2020] => 0
)
Actual result:
--------------
Array
(
[0] => 0
[2007] => 0
[2008] => 0
[2009] => 0
[2020] => 0
)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79084&edit=1