tyhoerr Posted May 12, 2011 Share Posted May 12, 2011 Hello, I'm trying to build a keyword search for a database of plants for a client... I'm not well versed in sql and the deadline is tomorrow to get this working... I've had no luck in the past 24 hours finding a solution on the internet. Basically what I want it to do is take a keyword (or series of keywords) and search the ENTIRE database for anything like those words. I was able to get it working for searching ONE column using this code: $find=ltrim($find); $find=rtrim($find); $kt=split(" ",$find); while(list($key,$val)=each($kt)){ if($val<>" " and strlen($val) > 0){$q .= " upper(Description) like '%$val%' or ";} } $q=substr($q,0,(strlen($q)-3)); $query="select * from Master_table where $q "; But this will only search the "Description" column, and not the other 15 columns of information. How can I code it to search through ALL of the columns and not just one of them? Thanks in advance for any help!! Quote Link to comment https://forums.phpfreaks.com/topic/236248-searching-all-columns-for-keyword/ Share on other sites More sharing options...
requinix Posted May 12, 2011 Share Posted May 12, 2011 Just repeat that same thing for the other columns. By the way, unless the columns are in a binary collation, LIKE is case-insensitive - you don't need UPPER(). $find = trim($find); $kt = preg_split('/\s+/', $find); $conditions = array(); foreach (array("col1", "col2", "col3", "col4") as $col) { foreach ($kt as $keyword) { $conditions[] = $col . " LIKE '%" . mysql_real_escape_string($keyword) . "%' "; } } $query = "SELECT * FROM Master_table "; if ($conditions) $query .= "WHERE " . implode("OR ", $conditions); Quote Link to comment https://forums.phpfreaks.com/topic/236248-searching-all-columns-for-keyword/#findComment-1214655 Share on other sites More sharing options...
tyhoerr Posted May 12, 2011 Author Share Posted May 12, 2011 That is PERFECT! Thank you so much for the quick response! Looks like an easy fix... obviously I still have much to learn about SQL Quote Link to comment https://forums.phpfreaks.com/topic/236248-searching-all-columns-for-keyword/#findComment-1214670 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.