Jump to content

Recommended Posts

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!!

Link to comment
https://forums.phpfreaks.com/topic/236248-searching-all-columns-for-keyword/
Share on other sites

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);

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.