Jump to content


Photo

Query troubles


  • Please log in to reply
8 replies to this topic

#1 Adthegreat

Adthegreat
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 06 May 2006 - 08:41 PM

Hey, I'm searching my database like this
$sql = "SELECT $choice FROM Member WHERE username = '$_SESSION[username]'";
$result = mysql_query($sql);
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
$row  = mysql_fetch_array($result,MYSQL_ASSOC);
extract($row);

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

What am I doing wrong?!

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 06 May 2006 - 09:23 PM

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

Legend has it that reading the manual never killed anyone.
My site

#3 Adthegreat

Adthegreat
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 06 May 2006 - 09:32 PM

[!--quoteo(post=371878:date=May 6 2006, 10:23 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ May 6 2006, 10:23 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
$sql = "SELECT choice FROM Member WHERE username = '$_SESSION[username]'";
[/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.

#4 twizler

twizler
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 06 May 2006 - 10:26 PM

User a variable in your sql statement:

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

make $choice = the users choice.

#5 Adthegreat

Adthegreat
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 06 May 2006 - 10:31 PM

[!--quoteo(post=371899:date=May 6 2006, 11:26 PM:name=twizler)--][div class=\'quotetop\']QUOTE(twizler @ May 6 2006, 11:26 PM) View Post[/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
extract($row);
echo "$$choice";
because that does not seem to work, but that is the syntax that i am familiar with.

#6 twizler

twizler
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 06 May 2006 - 10:39 PM


$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

#7 Adthegreat

Adthegreat
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 06 May 2006 - 10:56 PM

[!--quoteo(post=371903:date=May 6 2006, 11:39 PM:name=twizler)--][div class=\'quotetop\']QUOTE(twizler @ May 6 2006, 11:39 PM) View Post[/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
mysql_fetch_array($result,MYSQL_NUM);
echo "$row[0]";
does it say 1?!

And i cant do an associative array because i do not know what the rowtitle is because it is dynamic.

#8 twizler

twizler
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 06 May 2006 - 11:04 PM

Instead of: mysql_fetch_array($result,MYSQL_NUM);
Try: mysql_fetch_num($result);

#9 Adthegreat

Adthegreat
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 06 May 2006 - 11:11 PM

[!--quoteo(post=371911:date=May 7 2006, 12:04 AM:name=twizler)--][div class=\'quotetop\']QUOTE(twizler @ May 7 2006, 12:04 AM) View Post[/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..

$sql = "SELECT '.$choiceofpractice.' FROM Member WHERE username = '$_SESSION[username]'";
$result = mysql_query($sql);
$row  = mysql_fetch_num($result);
echo "$row[0]";





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users