Jump to content

[SOLVED] Possible Syntax Error in Query


Bopo

Recommended Posts

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");

Link to comment
Share on other sites

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%'");

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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  ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.