Jump to content

Query troubles


Adthegreat

Recommended Posts

Hey, I'm searching my database like this
[code]
$sql = "SELECT $choice FROM Member WHERE username = '$_SESSION[username]'";
$result = mysql_query($sql);
[/code]
Which will not work, it should find a percentage, but it always comes back with 1?!

I'm sure the problem is that i am searching primarily by $choice which is obviously a variable. Because when i search manually by editing the code from $choice to one of rows i get the right answer.

I'm trying loads of different ways of getting the data into an array but it always come out with the value 1.

And lastly, in attempts to solve the problem, i have echo'ed $choice and it has came out how it supposed to be, so no problems there.

I am trying to
[code]
$row  = mysql_fetch_array($result,MYSQL_ASSOC);
extract($row);

echo "$row[$choice]";
[/code]
and all i get is 1.

What am I doing wrong?!
Link to comment
Share on other sites

[!--quoteo(post=371878:date=May 6 2006, 10:23 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ May 6 2006, 10:23 PM) [snapback]371878[/snapback][/div][div class=\'quotemain\'][!--quotec--]
[code]$sql = "SELECT choice FROM Member WHERE username = '$_SESSION[username]'"; [/code]
[/quote]
'choice' must be dynamic. I didn't mention that choice is determined by a $_POST variable and then changed to $choice later.

Basically what the code does is takes the users choice, and then looks up information regarding their choice, to do other non-important things with. Because there are mutliple choices i must search by $choice.
Link to comment
Share on other sites

[!--quoteo(post=371899:date=May 6 2006, 11:26 PM:name=twizler)--][div class=\'quotetop\']QUOTE(twizler @ May 6 2006, 11:26 PM) [snapback]371899[/snapback][/div][div class=\'quotemain\'][!--quotec--]
User a variable in your sql statement:

$sql = "SELECT ".$choice." FROM Member WHERE username = '$_SESSION[username]'";

make $choice = the users choice.
[/quote]
Yeh mate, i am doing that, thats the problem, how do i get it into an array by that.

Is it
[code]
extract($row);
echo "$$choice";
[/code]
because that does not seem to work, but that is the syntax that i am familiar with.
Link to comment
Share on other sites


$query variable is your sql statement.

$result = mysql_query($query);
$arrayname = mysql_fetch_assoc($result)


To pull from an array do:

$arrayname['rowtitle'];


There are three different types to "fetch" from your query.
Assoc: Returns an associative array
Num: Return enumerated array
Both: Return Both
Link to comment
Share on other sites

[!--quoteo(post=371903:date=May 6 2006, 11:39 PM:name=twizler)--][div class=\'quotetop\']QUOTE(twizler @ May 6 2006, 11:39 PM) [snapback]371903[/snapback][/div][div class=\'quotemain\'][!--quotec--]
$query variable is your sql statement.

$result = mysql_query($query);
$arrayname = mysql_fetch_assoc($result)
To pull from an array do:

$arrayname['rowtitle'];
There are three different types to "fetch" from your query.
Assoc: Returns an associative array
Num: Return enumerated array
Both: Return Both
[/quote]
So why, when i put say
[code]
mysql_fetch_array($result,MYSQL_NUM);
echo "$row[0]";
[/code]
does it say 1?!

And i cant do an associative array because i do not know what the rowtitle is because it is dynamic.
Link to comment
Share on other sites

[!--quoteo(post=371911:date=May 7 2006, 12:04 AM:name=twizler)--][div class=\'quotetop\']QUOTE(twizler @ May 7 2006, 12:04 AM) [snapback]371911[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Instead of: mysql_fetch_array($result,MYSQL_NUM);
Try: mysql_fetch_num($result);
[/quote]
Fatal error: Call to undefined function: mysql_fetch_num()

I appreciate the fast replies.

This is my exact code now..

[code]
$sql = "SELECT '.$choiceofpractice.' FROM Member WHERE username = '$_SESSION[username]'";
$result = mysql_query($sql);
$row  = mysql_fetch_num($result);
echo "$row[0]";
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.