Jump to content

Search results screwed


benwhitmore

Recommended Posts

hi peeps

I have a very simple query that works fine:-

[code]

$query="SELECT id FROM stock WHERE category1 LIKE '%$search%' OR subcategory1 LIKE '%$search%'";
$query_res=mysql_query($query) or die(mysql_error());

[/code]

this works fine, the rest of the code returns values that matches my search criteria. If however, I add one more variable to my SELECT statement, PHP seems to ignore it. I want the SELECT statement to choose items where a field named "omit" has a value of "n":-

[code]

$query="SELECT id FROM stock WHERE omit='n' AND category1 LIKE '%$search%' OR subcategory1 LIKE '%$search%'";
$query_res=mysql_query($query) or die(mysql_error());

[/code]

I have items being returned where the value of "omit" = "y"

Have I used incorrect syntax in my SELECT statement?

any help will flatter my eyelids

Cheers
Link to comment
Share on other sites

I would guess this is all down to the use of AND and OR.

I believe that, at present, your statement retrives rows where ethier (omit=n and category1 is like $search ) OR (subcategory1 is like $search)

I hope the use of brackets there shows you what i mean; if omit=yes but subcatgeory1 is like $search, then the result will still be returned. Im not 100% on the solution, i have a feeling you might be able to use brackets in some way with the where statement, but i also think if you added on an extra part to the statement like this:

$query="SELECT id FROM stock WHERE omit='n' AND category1 LIKE '%$search%' OR subcategory1 LIKE '%$search%' AND omit='n'";

It might work. Dont hold me to it though ;)
Link to comment
Share on other sites

mate,

it took someone to look outside the box - you hit the nail on the head ole boy!!

OLD CODE:

[code]
"omit='n' AND category1 LIKE '%$search%' OR subcategory1 LIKE '%$search%'";
[/code]

NEW CODE:

[code]
"omit='n' AND category1 LIKE '%$search%' OR omit='n' AND subcategory1 LIKE '%$search%'";
[/code]

thanks again!!!!!

SOLVED
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.