Adthegreat Posted May 6, 2006 Share Posted May 6, 2006 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?! Quote Link to comment Share on other sites More sharing options...
AndyB Posted May 6, 2006 Share Posted May 6, 2006 [code]$sql = "SELECT choice FROM Member WHERE username = '$_SESSION[username]'"; [/code] Quote Link to comment Share on other sites More sharing options...
Adthegreat Posted May 6, 2006 Author Share Posted May 6, 2006 [!--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. Quote Link to comment Share on other sites More sharing options...
twizler Posted May 6, 2006 Share Posted May 6, 2006 User a variable in your sql statement:$sql = "SELECT ".$choice." FROM Member WHERE username = '$_SESSION[username]'";make $choice = the users choice. Quote Link to comment Share on other sites More sharing options...
Adthegreat Posted May 6, 2006 Author Share Posted May 6, 2006 [!--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. Quote Link to comment Share on other sites More sharing options...
twizler Posted May 6, 2006 Share Posted May 6, 2006 $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 arrayNum: Return enumerated arrayBoth: Return Both Quote Link to comment Share on other sites More sharing options...
Adthegreat Posted May 6, 2006 Author Share Posted May 6, 2006 [!--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 arrayNum: Return enumerated arrayBoth: 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. Quote Link to comment Share on other sites More sharing options...
twizler Posted May 6, 2006 Share Posted May 6, 2006 Instead of: mysql_fetch_array($result,MYSQL_NUM);Try: mysql_fetch_num($result); Quote Link to comment Share on other sites More sharing options...
Adthegreat Posted May 6, 2006 Author Share Posted May 6, 2006 [!--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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.