php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #16029 Feature Request on mysql_fetch_array()
Submitted: 2002-03-12 19:55 UTC Modified: 2002-03-12 20:09 UTC
From: mjm at porter dot appstate dot edu Assigned:
Status: Closed Package: MySQL related
PHP Version: 4.1.2 OS: Linux 2.4 (intel)
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: mjm at porter dot appstate dot edu
New email:
PHP Version: OS:

 

 [2002-03-12 19:55 UTC] mjm at porter dot appstate dot edu
When you SELECT two columns from different tables with the same column name in a JOIN, you cannot use column names is associative array keys. It would be really nice when you did something like this:
-------
$result = mysql_query("SELECT tbl1.colA, tbl2.colA FROM tbl1 LEFT JOIN tbl2 ON tbl1.colB = tbl2.colB");
$row = mysql_fetch_array($result);
-------
if you could then do something like this:
-------
$mydata1 = $row["tbl1.colA"];
$mydata1 = $row["tbl2.colA"];
-------
With the current behavior, you may only access the second array element with its numerical index.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-03-12 20:09 UTC] torben@php.net
The data is returned from the server like that. What you can
do is:

'select tbl1.colA as tbl1_colA, tbl2.colA as tbl2_colA. . .'


Torben

 [2002-04-25 04:34 UTC] mderk at yandex dot ru
That is not true. All the fields are returned from the server, and one can fetch the field name, if he knows the number of the field in the returned row. And when php_mysql_fetch_hash() comes into play, it uses add_assoc_stringl() to place the result fields into array or object that returned by mysql_fetch_array() and friends. Unfortunately, add_assoc_stringl() in this case simply rewrites the value of the field colA, coming from tbl1 with the value of the field with the same name coming from tbl2, because they would have the same associative key in the resulting hash. 
I saw some clever client libraries that avoid this rewrite - they verify first if the field with the name of the current field is present in the resulting hash/recordset, and then, if it is true, append a number to the current field name to store it with the new key.
E.g., ELECT tbl1.colA, tbl2.colA FROM tbl1 LEFT JOIN
tbl2 ON tbl1.colB = tbl2.colB could end up in array with keys 'colA' for tbl1.colA and 'colA_1' for tbl2.colA.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Sun Mar 22 12:00:01 2026 UTC