Bopo Posted November 3, 2008 Share Posted November 3, 2008 Hey I have wrote this query for php, however I am not sure if its valid to use so many clauses, or possibly I might have a syntax error, I have tried a few different things such as double quotes around the varible, and percentage signs but no luck so far, help appreciated. $cartype = "BMW 1 Series"; $result = mysql_query("SELECT * FROM cardata WHERE model LIKE = '$cartype' LIMIT 1"); Quote Link to comment https://forums.phpfreaks.com/topic/131216-solved-possible-syntax-error-in-query/ Share on other sites More sharing options...
rhodesa Posted November 3, 2008 Share Posted November 3, 2008 depends...do you want it to be an exact match? so the 'model' field is equal to the string in $cartype? if so, use: $cartype = "BMW 1 Series"; $result = mysql_query("SELECT * FROM cardata WHERE model = '$cartype' LIMIT 1"); LIKE should be used when you want to (for instance) find all the models with BMW in the name: $cartype = "BMW"; $result = mysql_query("SELECT * FROM cardata WHERE model LIKE '%$cartype%'"); Quote Link to comment https://forums.phpfreaks.com/topic/131216-solved-possible-syntax-error-in-query/#findComment-681227 Share on other sites More sharing options...
Bopo Posted November 3, 2008 Author Share Posted November 3, 2008 Hi and thanks for you help The Model column doesn't have 'BMW 1 Series' on its own anywhere, and this applies to all the other vechiles within it, for example BMW 1 Series 2dr coupe BMW 1 Series 2dr convertible BMW 1 Series 3dr hatchback BMW 1 Series 5dr hatchback So I would like the query to retrive the first record it finds with the words 'BMW 1 Series' within the Model column, I know this might seen vague, but this would be suitable for what I am doing. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/131216-solved-possible-syntax-error-in-query/#findComment-681267 Share on other sites More sharing options...
Maq Posted November 3, 2008 Share Posted November 3, 2008 $cartype = "BMW 1 Series"; $result = mysql_query("SELECT * FROM cardata WHERE model LIKE '%$cartype%' LIMIT 1"); Quote Link to comment https://forums.phpfreaks.com/topic/131216-solved-possible-syntax-error-in-query/#findComment-681270 Share on other sites More sharing options...
rhodesa Posted November 3, 2008 Share Posted November 3, 2008 leave the % off the front of the string if you don't need a wildcard there... $cartype = "BMW 1 Series"; $result = mysql_query("SELECT * FROM cardata WHERE model LIKE '$cartype%' LIMIT 1"); Quote Link to comment https://forums.phpfreaks.com/topic/131216-solved-possible-syntax-error-in-query/#findComment-681312 Share on other sites More sharing options...
Maq Posted November 3, 2008 Share Posted November 3, 2008 leave the % off the front of the string if you don't need a wildcard there... $cartype = "BMW 1 Series"; $result = mysql_query("SELECT * FROM cardata WHERE model LIKE '$cartype%' LIMIT 1"); Thanks rhodesa Quote Link to comment https://forums.phpfreaks.com/topic/131216-solved-possible-syntax-error-in-query/#findComment-681317 Share on other sites More sharing options...
Bopo Posted November 3, 2008 Author Share Posted November 3, 2008 Works perfectly, cheers Quote Link to comment https://forums.phpfreaks.com/topic/131216-solved-possible-syntax-error-in-query/#findComment-681329 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.