Jump to content

OR in a search query?


dmccabe

Recommended Posts

OK so I worked that bit out:

 

SELECT * FROM `tbl_corpaccount` WHERE `accname` OR `accno` LIKE 'Test%'

 

The only problem being that it wont work with the OR, however if I remove the OR and `accno` then it works, but I need it search both.

Link to comment
Share on other sites

The answer is: using a LIKE clause with a % (which is an asterisk equivalent)

e.g.

SELECT * FROM `tbl_corpaccount` WHERE `accname` OR `accno` LIKE '$search%' ORDER BY aid ASC

 

I also noted another problem. Your logic is wrong.

"WHERE `accname` OR ..." will ALWAYS be true.

It needs to be "WHERE `accname` = 'blah' OR ..."

 

I have another question though, isn't `accno` going to be a number?

Link to comment
Share on other sites

Ok here is current code as I think we are confusing matters:

 

$query = "SELECT * FROM `tbl_corpaccount` WHERE `accname` OR `accno` LIKE '%$search%' ORDER BY aid ASC";

 

This doesnt work when I put it direct in to SQL query in phpmyadmin it returns 0 results.

 

however if I remove the OR `accno` section it works perfectly?

 

 

Link to comment
Share on other sites

It's because there is one search box and I want them to be able to either search for the account name or account number (which is letters and numbers VARCHAR).

 

So it searches both fields so that they can search for either.

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.