0

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;

}


}
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://stackoverflow.com/a/14110189/1723893). – NullPoiиteя Jan 13 '13 at 16:33

2 Answers2

1

Was this written in PHP? What type of Database are you connecting too? These bits of information would be helpful! Sometimes different databases can be picky about how thier queries are structured!

Never the less, try this code and see if it creates the desired results:

foreach($keywords as $key=>$keyword) {
    $where .= "(   `company_name`  LIKE  '%$keyword%'   "
           .  " OR `categories`    LIKE  '%$keyword%'  )";
    if($key != ($total_keywords - 1)) {
        $where .= " AND ";

    }
}
recursion.ninja
  • 5,377
  • 7
  • 46
  • 78
  • Thanks, your answer worked for me. But, it is clearly shown in the code that it was Mysql database. – samayo Nov 04 '12 at 02:02
1

you can use an OR in this case with parentheses like that

$where .= "(`company_name`  LIKE  '%$keyword%' OR `category`  LIKE  '%$keyword%')   ";
EmeraldCoder
  • 527
  • 3
  • 12