imperium2335 Posted August 26, 2010 Share Posted August 26, 2010 How do I get LIKE to check multiple values? It works if you type in a single word, but not multiple, which I want it to in order to get an accurate search result. Here is what I am trying to do: $target = "siemens 6s" ; $multis = explode(" ", $target) ; $query = "SELECT parts_table.*, manufacturers.* FROM parts_table, manufacturers WHERE parts_table.ManufacturerID = manufacturers.ManufacturerID AND parts_table.PartNumber LIKE" ; foreach($multis as $current) { $queryext .= " '%$current%' OR" ; } $queryext = substr($queryext, 0, -3) ; // Remove the last 4 charecters i.e. " AND". $query .= $queryext ; echo $query .= " OR manufacturers.ManufacturerID = parts_table.ManufacturerID AND manufacturers.ManufacturerName LIKE" . $queryext ; include("dbconnect.php") ; $result = mysql_query($query) ; while($row = mysql_fetch_assoc($result)){ echo $row['ManufacturerName'] ; echo $row['PartNumber'] ; } Quote Link to comment https://forums.phpfreaks.com/topic/211791-mysql-multiple-like-not-working/ Share on other sites More sharing options...
bh Posted August 26, 2010 Share Posted August 26, 2010 in your foreach at $queryext .= " '%$current%' OR" ; after the first cycle you havent got 'LIKE' before your '%$current%' .... Quote Link to comment https://forums.phpfreaks.com/topic/211791-mysql-multiple-like-not-working/#findComment-1103962 Share on other sites More sharing options...
imperium2335 Posted August 26, 2010 Author Share Posted August 26, 2010 Not sure what you mean, if i echo the finished query it looks like this: SELECT parts_table.*, manufacturers.* FROM parts_table, manufacturers WHERE parts_table.ManufacturerID = manufacturers.ManufacturerID AND parts_table.PartNumber LIKE '%siemens%' OR '%6s% OR manufacturers.ManufacturerID = parts_table.ManufacturerID AND manufacturers.ManufacturerName LIKE '%siemens%' OR '%6s% Quote Link to comment https://forums.phpfreaks.com/topic/211791-mysql-multiple-like-not-working/#findComment-1103964 Share on other sites More sharing options...
iSE Posted August 26, 2010 Share Posted August 26, 2010 it should be WHERE field = value OR field = value OR field = value... so you should have WHERE field LIKE value OR field LIKE value as LIKE as an opperand. Quote Link to comment https://forums.phpfreaks.com/topic/211791-mysql-multiple-like-not-working/#findComment-1103966 Share on other sites More sharing options...
bh Posted August 26, 2010 Share Posted August 26, 2010 LIKE '%siemens%' OR '%6s% OR manufacturers.ManufacturerID = parts_table.ManufacturerID AND manufacturers.ManufacturerName LIKE '%siemens%' OR '%6s% Well, wheres your 'LIKE' clause eg. before your %6s% ? Quote Link to comment https://forums.phpfreaks.com/topic/211791-mysql-multiple-like-not-working/#findComment-1103967 Share on other sites More sharing options...
imperium2335 Posted August 26, 2010 Author Share Posted August 26, 2010 Thanks! but it still doesnt work : SELECT parts_table.*, manufacturers.* FROM parts_table, manufacturers WHERE parts_table.ManufacturerID = manufacturers.ManufacturerID AND parts_table.PartNumber LIKE '%siemens%' OR LIKE '%6SN%' OR manufacturers.ManufacturerID = parts_table.ManufacturerID AND manufacturers.ManufacturerName LIKE '%siemens%' OR LIKE '%6SN%' Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in part-search-new.php on line 33 Quote Link to comment https://forums.phpfreaks.com/topic/211791-mysql-multiple-like-not-working/#findComment-1103972 Share on other sites More sharing options...
imperium2335 Posted August 26, 2010 Author Share Posted August 26, 2010 Is there any way to match and compare substrings in mysql queries as im having a hard time getting my search function to work if the user types in more than one word? Quote Link to comment https://forums.phpfreaks.com/topic/211791-mysql-multiple-like-not-working/#findComment-1103978 Share on other sites More sharing options...
imperium2335 Posted August 26, 2010 Author Share Posted August 26, 2010 I have also just tried this with no succes: SELECT parts_table.*, manufacturers.* FROM parts_table, manufacturers WHERE parts_table.ManufacturerID = manufacturers.ManufacturerID AND WHERE 'siemens 6SN' IN parts_table.PartNumber OR manufacturers.ManufacturerID = parts_table.ManufacturerID AND WHERE 'siemens 6SN IN manufacturers.ManufacturerName Quote Link to comment https://forums.phpfreaks.com/topic/211791-mysql-multiple-like-not-working/#findComment-1103985 Share on other sites More sharing options...
iSE Posted August 26, 2010 Share Posted August 26, 2010 The format of your statements must be SELECT cols FROM table WHERE field OPERAND value LOGIC field OPERAND value LOGIC field OPERAND value... So... SELECT parts_table.*, manufacturers.* FROM parts_table, manufacturers WHERE parts_table.ManufacturerID = manufacturers.ManufacturerID AND parts_table.PartNumber LIKE '%siemens%' OR parts_table.PartNumber LIKE '%6SN%' OR manufacturers.ManufacturerID = parts_table.ManufacturerID AND manufacturers.ManufacturerName LIKE '%siemens%' OR manufacturers.ManufacturerName LIKE '%6SN%' That work? Quote Link to comment https://forums.phpfreaks.com/topic/211791-mysql-multiple-like-not-working/#findComment-1104044 Share on other sites More sharing options...
imperium2335 Posted August 27, 2010 Author Share Posted August 27, 2010 No, now it's bringing back all the stuff with "siemens" in: SELECT parts_table.*, manufacturers.* FROM parts_table, manufacturers WHERE parts_table.ManufacturerID = manufacturers.ManufacturerID AND parts_table.PartNumber LIKE '%siemens%' OR parts_table.PartNumber LIKE '%6SN2460-2CF00-0GB0%' OR manufacturers.ManufacturerID = parts_table.ManufacturerID AND manufacturers.ManufacturerName LIKE '%siemens%' OR parts_table.PartNumber LIKE '%6SN2460-2CF00-0GB0%' I just want it to bring back "siemens 6SN2460-2CF00-0GB0" i.e. 1 part (which there is only one of). Typing in just 6SN2460-2CF00-0GB0 will bring back one record like it should. Siemens will bring back all siemens parts which is correct to. But "siemens 6SN2460-2CF00-0GB0" brings back the same results as typing in just siemens. Any idea how I can get it to work properly? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/211791-mysql-multiple-like-not-working/#findComment-1104267 Share on other sites More sharing options...
DavidAM Posted August 27, 2010 Share Posted August 27, 2010 If you are going to mix AND with OR you have to mark the groups that go together (using parenthesis): SELECT parts_table.*, manufacturers.* FROM parts_table, manufacturers WHERE (parts_table.ManufacturerID = manufacturers.ManufacturerID AND (parts_table.PartNumber LIKE '%siemens%' OR parts_table.PartNumber LIKE '%6SN2460-2CF00-0GB0%')) OR (manufacturers.ManufacturerID = parts_table.ManufacturerID AND (manufacturers.ManufacturerName LIKE '%siemens%' OR parts_table.PartNumber LIKE '%6SN2460-2CF00-0GB0%')) Actually, if you use a JOIN here, you can eliminate that issue since the ManufacturerID comes out of the WHERE clause: SELECT parts_table.*, manufacturers.* FROM parts_table JOIN manufacturers ON parts_table.ManufacturerID = manufacturers.ManufacturerID WHERE parts_table.PartNumber LIKE '%siemens%' OR parts_table.PartNumber LIKE '%6SN2460-2CF00-0GB0%' OR manufacturers.ManufacturerName LIKE '%siemens%' Quote Link to comment https://forums.phpfreaks.com/topic/211791-mysql-multiple-like-not-working/#findComment-1104457 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.