Jump to content

Archived

This topic is now archived and is closed to further replies.

doni49

Query returns column name instead of row values

Recommended Posts

This is a code snippet from my PHP file.
[code]
        $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>";
        }        
[/code]

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--]
[b]id user_id tblName setName fields
[/b]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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.