php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45237 Missing values on mysql_fetch_assoc
Submitted: 2008-06-11 13:33 UTC Modified: 2008-06-11 17:20 UTC
From: phpbugs at phormix dot com Assigned:
Status: Not a bug Package: MySQL related
PHP Version: 5.2.6 OS: FreeBSD
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
7 - 6 = ?
Subscribe to this entry?

 
 [2008-06-11 13:33 UTC] phpbugs at phormix dot com
Description:
------------
mysql_fetch_assoc does not return certain values attached to their named items in the array. mysql_fetch_array shows that the numbered entries definitely have values attached to them, but for some reason these are not being attached to the appropriate names.

For example, as below. The first field is "userid"
array[0] returns a value
array[userid] does not

however, when adding code to the query "userid as userid2"
array[0] returns a value
array[userid] does not
array[1] returns a value
array[userid2] returns a value

Reproduce code:
---------------
$myquery = "SELECT user.userid, user.userid as userid2, reputation, username, usergroupid, birthday_search, email, parentemail,(options & 8) AS coppauser, homepage, icq, aim, yahoo, msn, skype, signature, usertitle, joindate, lastpost, posts, ipaddress, lastactivity, userfield.* FROM user LEFT JOIN userfield AS userfield ON(userfield.userid = user.userid) LEFT JOIN usertextfield AS usertextfield ON(usertextfield.userid = user.userid) WHERE 1=1 AND user.username LIKE '%jdoe%' ORDER BY username ASC LIMIT 0, 50";

$user_r = mysql_query($myquery);
print_r( mysql_fetch_array($user_r) );




Expected result:
----------------
Array ( [0] => 22768 [userid] => [1] => 22768 [userid2] => 22768 [2] => 10 [reputation] => 10 [3] => jdoe [username] => jdoe [4] ... etc

Actual result:
--------------
Array ( [0] => 22768 [userid] => [1] => 22768 [userid2] => 22768 [2] => 10 [reputation] => 10 [3] => jdoe [username] => jdoe [4] ... etc

(please note the missing userid)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-06-11 13:34 UTC] phpbugs at phormix dot com
Please note that the "expected" result should actually be:

Array ( [0] => 22768 [userid] => 22768 [1] => 22768 [userid2] => 22768 [2] =>
10 [reputation] => 10 [3] => jdoe [username] => jdoe [4] ... etc
 [2008-06-11 13:40 UTC] johannes@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're using left joins, therefore you get NULL values. Use vardump() instead of print_r() to get the the information.
 [2008-06-11 13:41 UTC] johannes@php.net
var_dump, not vardump, sorry for that
 [2008-06-11 16:39 UTC] phpbugs at phormix dot com
OK, this seems like odd behavior to me then, and not a JOIN-related issue because of the following:

a) The value in question is part of the primary selected table (user) and not one of the joined fields

b) Other values in this table are returning properly

c) The second variable, userid2, is the SAME field, but just aliased to a new name using "user.userid as userid2"

d) The values return fine when using a numeric array, it is only through the name of the associative array that it doesn't work


So perhaps I'm being dumb. This isn't my query (it's part of a bulletin-board system), but I can't find any reasons, given the above, that it should be returning results in this manner, and I've been working with PHP+MySQL for quite awhile.
 [2008-06-11 16:44 UTC] phpbugs at phormix dot com
Just to clarify this, if it were caused by a bad join, would it not also mean that array[0] should be NULL, as well as the aliased array[userid2]?
 [2008-06-11 17:20 UTC] phpbugs at phormix dot com
FYI,


Same query, when executed through the mysql console, returns a value for fields "userid" and "userid2"
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 10:01:26 2024 UTC