php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40940 mysql_fetch_array returns different values for ASSOC and NUM
Submitted: 2007-03-29 00:55 UTC Modified: 2007-03-29 09:29 UTC
From: dgk at visskiss dot org Assigned:
Status: Not a bug Package: MySQL related
PHP Version: 5.2.1 OS: OS X
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: dgk at visskiss dot org
New email:
PHP Version: OS:

 

 [2007-03-29 00:55 UTC] dgk at visskiss dot org
Description:
------------
mysql_fetch_array returns different values for MYSQL_NUM and MYSQL_ASSOC

Reproduce code:
---------------
$query = "SELECT table1.*, table2.* FROM table1 LEFT JOIN table2 ON table1.id = table2.id WHERE table1.data='foo'";
$result = mysql_query($query);
$row=mysql_fetch_array($result);

$row['id'] = NULL; //(there is no corresponding row for left join)
$row[0] = 2 //there is a value for id from table1...



Expected result:
----------------
same value for associative and numerical element

Actual result:
--------------
assoc picks up one value, num another...

seems only to occur for left joins where there is a null row for join 
criteria....

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-29 03:29 UTC] btherl at yahoo dot com dot au
This is not a bug.  From the manual:

"If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you must use the numeric index of the column or make an alias for the column. For aliased columns, you cannot access the contents with the original column name."

In this case, the last "id" will be from the right table, whereas index 0 will be from the left table.
 [2007-03-29 06:07 UTC] georg@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

You should write cleaner code and avoid duplicate column names in associative arrays.
 [2007-03-29 09:29 UTC] dgk at visskiss dot org
Hi,

As  you can see, I figured out a work around (write cleaner code)!  But 
I don't think this is as much a feature as an inconsistency.  What 
possible benefit could it be to have the numeric and assoc elements be 
different?  

Agreed, tje code should be cleaner, but you should just put the last 
element in both assoc and numerical elements...

Thanks again!
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Aug 16 09:00:02 2025 UTC