Jump to content


Photo

Query returns column name instead of row values


  • Please log in to reply
4 replies to this topic

#1 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 02 March 2006 - 02:23 AM

This is a code snippet from my PHP file.
        $query = "SELECT 'setName', 'fields' FROM userFieldSets WHERE user_id='$uid'";
        $result = @mysql_query ($query) or die ('I cannot get fields from the database because: ' . mysql_error());
        $row = mysql_fetch_array ($result, MYSQL_NUM);

        if ($row) { // A match was made.
            echo "0:  " . $row[0] . "<br>";
            echo "1:  " . $row[1] . "<br>";
        }        

It shows this in my browser.
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
0: setName
1: fields
[/quote]

This is copy/pasted from phpMyAdmin.
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
id user_id tblName setName fields
1 1 mainIndex Default 1,2,3,4,5,6,7,8,10,13,14
2 1 propertyAmtsDue Default 1,2,3,4,5,6,7,8,10,13,14
3 1 propertyContact Default 1,2,3,4,5,6

[/quote]


Don

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 02 March 2006 - 03:00 AM

You're quoting your column names, so they're being interpreted by the parser as string literals (hence the output). Drop the quotes, and you're off.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 02 March 2006 - 03:16 AM

That's strange. I only put them in there because I was getting error messages that there was something wrong with the syntax.

When I put them in the error message went away.

Is "Fields" a reserved name or something?

While waiting to hear from someone, I kept trying different things. I changed "Fields" to "Flds" and still got the column names anyway. But then as soon as I took the quotes back out, it worked fine (with it still using "Flds").

Thanks for the help.
Don

#4 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 02 March 2006 - 04:07 AM

For future reference:

[a href=\"http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html\" target=\"_blank\"]http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html[/a]

#5 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 02 March 2006 - 03:04 PM

Yes, FIELDS is reserved -- the "correct" way to request such columns is with backticks (e.g. `fields`); however, the best practice is to simply avoid using these to begin with. wickning1 give you a link to a list of these.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users