twistedmando Posted December 18, 2006 Share Posted December 18, 2006 I must be looking in the wrong places for answers, but I'm not finding anything. My Web host is using MySQLv5, and PHP5. The following is the offending code.// The value of $color is 'Red'$extract="SELECT amount FROM colors WHERE color = $color";$amount=mysql_query($extract) or die(mysql_error());With this code I get a notification of"Unknown column 'Red' in 'where clause'"But my field is called colorIf I try '$color', or '"$color"', or '{$color}' etc. I get syntax errors.Clearly what I want is to get the value of amount from the entry where color is Red.Any suggestions?twistedmando Quote Link to comment https://forums.phpfreaks.com/topic/31120-solved-php-string-variables-in-a-mysql-clause/ Share on other sites More sharing options...
drifter Posted December 18, 2006 Share Posted December 18, 2006 $extract="SELECT amount FROM colors WHERE color = '" . $color . "'"; Quote Link to comment https://forums.phpfreaks.com/topic/31120-solved-php-string-variables-in-a-mysql-clause/#findComment-143712 Share on other sites More sharing options...
HuggieBear Posted December 18, 2006 Share Posted December 18, 2006 You need single quotes around your value...[color=blue]$extract="SELECT amount FROM colors WHERE color = [color=red]'[/color]$color[color=red]'[/color]";[/color]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31120-solved-php-string-variables-in-a-mysql-clause/#findComment-143713 Share on other sites More sharing options...
twistedmando Posted December 18, 2006 Author Share Posted December 18, 2006 HMMWhen I try $extract="SELECT amount FROM colors WHERE color = '" . $color . "'";$amount=mysql_query($extract) or die(mysql_error());or$extract="SELECT amount FROM colors WHERE color = '$color' ";$amount=mysql_query($extract) or die(mysql_error());I get the following error message.You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'id #3 WHERE color = 'Red'' at line 1I'm making sure to start with a new browser each time I test.Thanks for looking!I'm stumped. Quote Link to comment https://forums.phpfreaks.com/topic/31120-solved-php-string-variables-in-a-mysql-clause/#findComment-143726 Share on other sites More sharing options...
drifter Posted December 18, 2006 Share Posted December 18, 2006 this is an SQL error - do you have a column names color and amount in a table named colors? Quote Link to comment https://forums.phpfreaks.com/topic/31120-solved-php-string-variables-in-a-mysql-clause/#findComment-143729 Share on other sites More sharing options...
twistedmando Posted December 18, 2006 Author Share Posted December 18, 2006 Yes, I have a column named color and a column named amount in a table called colors. The database is also called colors. Quote Link to comment https://forums.phpfreaks.com/topic/31120-solved-php-string-variables-in-a-mysql-clause/#findComment-143741 Share on other sites More sharing options...
twistedmando Posted December 18, 2006 Author Share Posted December 18, 2006 I changed my table name to my_colors, and I still get the same error. Quote Link to comment https://forums.phpfreaks.com/topic/31120-solved-php-string-variables-in-a-mysql-clause/#findComment-143763 Share on other sites More sharing options...
twistedmando Posted December 18, 2006 Author Share Posted December 18, 2006 Still no succes. Anybody have a guess? Quote Link to comment https://forums.phpfreaks.com/topic/31120-solved-php-string-variables-in-a-mysql-clause/#findComment-143838 Share on other sites More sharing options...
HuggieBear Posted December 18, 2006 Share Posted December 18, 2006 If you're trying to get the amount then you need to fetch that data too. Try this code (error checking included this time)...[code]<?php$sql = "SELECT amount FROM colors WHERE color = '$color'";$result = mysql_query($sql);if (!$result){ echo "Unable to run query: $sql<br>\n" . mysql_error();}else { $row = mysql_fetch_array($result, MYSQL_ASSOC); echo $row['amount'];}?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31120-solved-php-string-variables-in-a-mysql-clause/#findComment-143882 Share on other sites More sharing options...
twistedmando Posted December 18, 2006 Author Share Posted December 18, 2006 ;DSweet! Thanks HuggieBear!Twistedmando Quote Link to comment https://forums.phpfreaks.com/topic/31120-solved-php-string-variables-in-a-mysql-clause/#findComment-143921 Share on other sites More sharing options...
HuggieBear Posted December 18, 2006 Share Posted December 18, 2006 If that worked then mark the topic as solved so others can take advice from it.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31120-solved-php-string-variables-in-a-mysql-clause/#findComment-143929 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.