Sorry about the confusing title, I am newbie and have no idea what to call my problem. I am building (well..trying) to create a business listing website and I found this search script, which gets results from table. So, I pasted this, tried it and it works, but only searches one field at a time, I can't search two fields at the same time. only data from field: "company_name" is retuned. But, I need to make this flexible and append it to search from field name 'categories' so, I in short I need PHP to search two rows i.e. 'company_name' & 'categories' field.
Here is the script, for which I don't know how to add the additional syntax for.
$keywords = preg_split('/[\s]+/',$keywords);
$total_keywords = count($keywords);
foreach($keywords as $key=>$keyword) {
$where .= "`company_name` LIKE '%$keyword%' ";
if($key != ($total_keywords - 1)) {
$where .= " AND ";
}
the problem is in here: $where .= "
company_nameLIKE '%$keyword%' ";
if you need to see the whole script here it is.
function search_clients($keywords){
$returned_results = array();
$where = "";
$keywords = preg_split('/[\s]+/',$keywords);
$total_keywords = count($keywords);
foreach($keywords as $key=>$keyword) {
$where .= "`company_name` LIKE '%$keyword%' ";
if($key != ($total_keywords - 1)) {
$where .= " AND ";
}
}
$results = "SELECT `company_name`, LEFT(`company_details`,150)
as `company_details`, `category` FROM `clients`
WHERE $where";
$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) : 0 ;
if($results_num === 0) {
return false;
} else {
while($results_row = mysql_fetch_assoc($results)) {
$returned_results[] = array(
'company_name' => $results_row['company_name'],
'company_details' => $results_row['company_details'],
);
}
return $returned_results;
}
}