dmccabe Posted March 31, 2008 Share Posted March 31, 2008 Can you use OR in a search query? eg $query = "SELECT * FROM `tbl_corpaccount` WHERE `accname` OR `accno` = $search* ORDER BY aid ASC"; Quote Link to comment Share on other sites More sharing options...
aschk Posted March 31, 2008 Share Posted March 31, 2008 Yes, but you'll need make sure your string is inside single quotes (') and the asterisk (*) symbol is invalid. Quote Link to comment Share on other sites More sharing options...
dmccabe Posted March 31, 2008 Author Share Posted March 31, 2008 thanks. The * symbol was my next question, how do I make it so if they type in "test" then it searches for "test*" (so anything beginning with test) ? Quote Link to comment Share on other sites More sharing options...
dmccabe Posted March 31, 2008 Author Share Posted March 31, 2008 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. Quote Link to comment Share on other sites More sharing options...
aschk Posted March 31, 2008 Share Posted March 31, 2008 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? Quote Link to comment Share on other sites More sharing options...
dmccabe Posted March 31, 2008 Author Share Posted March 31, 2008 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? Quote Link to comment Share on other sites More sharing options...
dmccabe Posted March 31, 2008 Author Share Posted March 31, 2008 ok this works: SELECT * FROM `tbl_corpaccount` WHERE `accname` LIKE 'test%' OR `accno` LIKE 'test%' Quote Link to comment Share on other sites More sharing options...
aschk Posted April 1, 2008 Share Posted April 1, 2008 Mmm, it seems a bit silly that you're checking 2 fields for the same information? I would have thought accno to be an INT or numeric type of some sort. Quote Link to comment Share on other sites More sharing options...
dmccabe Posted April 1, 2008 Author Share Posted April 1, 2008 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. Quote Link to comment Share on other sites More sharing options...
aschk Posted April 1, 2008 Share Posted April 1, 2008 Ah fair enough, semantics mean a lot to me these days so I like to know what the "literal" representation of the column also has meaning about the data contained within. Quote Link to comment 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.