spmckee Posted March 23, 2008 Share Posted March 23, 2008 Greetings, I have a data field in mySQL that reads "Fish and Shellfish", when I try to match it with the same string I get this error: Error in query: SELECT `cat_id` FROM `categories` WHERE `cat_name` = Fish and Shellfish. Unknown column 'Fish' in 'where clause Basically it breaks when it hits the whitespace in the field data. Any ideas? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/97444-error-matching-field-data/ Share on other sites More sharing options...
uniflare Posted March 23, 2008 Share Posted March 23, 2008 you need to encapsulate string in mysql with single quotes, eg: SELECT `cat_id` FROM `categories` WHERE `cat_name` = Fish and Shellfish should be SELECT `cat_id` FROM `categories` WHERE `cat_name` = 'Fish and Shellfish' Quote Link to comment https://forums.phpfreaks.com/topic/97444-error-matching-field-data/#findComment-498581 Share on other sites More sharing options...
ohdang888 Posted March 23, 2008 Share Posted March 23, 2008 your query must be this mysql_query("SELECT `cat_id` FROM `categories` WHERE `cat_name` = 'Fish and Shellfish' ")or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/97444-error-matching-field-data/#findComment-498582 Share on other sites More sharing options...
uniflare Posted March 23, 2008 Share Posted March 23, 2008 oops, also you will prbably need to do something like this instead: SELECT `cat_id` FROM `categories` WHERE `cat_name` = 'Fish' OR `cat_name` = 'Shellfish' or you could even: SELECT `cat_id` FROM `categories` WHERE `cat_name` LIKE '%fish%' (i believe most mysql installations are case sensitive, you need to change the mysql character table to make it insensitive) hope this helps, Quote Link to comment https://forums.phpfreaks.com/topic/97444-error-matching-field-data/#findComment-498584 Share on other sites More sharing options...
spmckee Posted March 23, 2008 Author Share Posted March 23, 2008 Thanks for the swift reply but I think it will make it clearer if I include the whole piece. This is part of a function: function get_cat_id($which_one){ $query = "SELECT `cat_id` FROM `categories` WHERE `cat_name` = $which_one"; $result=mysql_query($query) or die ("Error in query: $query. " . mysql_error()); while($row = mysql_fetch_array($result)) {$which_one=$row['cat_id'];}; return $which_one; } How do I translate your tips into the function? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/97444-error-matching-field-data/#findComment-498585 Share on other sites More sharing options...
uniflare Posted March 23, 2008 Share Posted March 23, 2008 what exactly do you want this function to return? Quote Link to comment https://forums.phpfreaks.com/topic/97444-error-matching-field-data/#findComment-498897 Share on other sites More sharing options...
MadTechie Posted March 23, 2008 Share Posted March 23, 2008 change $query = "SELECT `cat_id` FROM `categories` WHERE `cat_name` = $which_one"; to $query = "SELECT `cat_id` FROM `categories` WHERE `cat_name` = '$which_one' "; or $query = "SELECT `cat_id` FROM `categories` WHERE `cat_name` LIKE '%$which_one%' "; Quote Link to comment https://forums.phpfreaks.com/topic/97444-error-matching-field-data/#findComment-498904 Share on other sites More sharing options...
spmckee Posted March 24, 2008 Author Share Posted March 24, 2008 Thanks for the help all. MadTechie, you got it straightened out for me. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/97444-error-matching-field-data/#findComment-499435 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.