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"; Link to comment https://forums.phpfreaks.com/topic/98801-or-in-a-search-query/ 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. Link to comment https://forums.phpfreaks.com/topic/98801-or-in-a-search-query/#findComment-505560 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) ? Link to comment https://forums.phpfreaks.com/topic/98801-or-in-a-search-query/#findComment-505564 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. Link to comment https://forums.phpfreaks.com/topic/98801-or-in-a-search-query/#findComment-505572 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? Link to comment https://forums.phpfreaks.com/topic/98801-or-in-a-search-query/#findComment-505574 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? Link to comment https://forums.phpfreaks.com/topic/98801-or-in-a-search-query/#findComment-505585 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%' Link to comment https://forums.phpfreaks.com/topic/98801-or-in-a-search-query/#findComment-505596 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. Link to comment https://forums.phpfreaks.com/topic/98801-or-in-a-search-query/#findComment-506278 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. Link to comment https://forums.phpfreaks.com/topic/98801-or-in-a-search-query/#findComment-506327 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. Link to comment https://forums.phpfreaks.com/topic/98801-or-in-a-search-query/#findComment-506333 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.