如何通过使用php通过文本字段发送多个关键词来搜索mysql数据库

  1. For example: in a database table there are few columns

    id pname pcolor size mrp image . 1 computer
    black 18inch 15000 comp1.jpg 2 cup silver
    6inch 15 cup.jpg 3 computer white 12inch 7000
    compt2.jpg

    there is a table like above. now i am going to search a product by sending through a text field as bellow:

    search : computers black color.

    how should i get the particular row from data base please tell me the code.

    = 1"); //$result = mysql_query("select * from proddetail where (pname = 'computer')"); //or(pKey LIKE '%".$key."%' OR pname LIKE '%".$key."%' OR mfg LIKE '%".$key."%' OR psize LIKE '%".$key."%' OR mrp LIKE '%".$key."%' OR saleprize LIKE '%".$key."%' OR pType LIKE '%".$key."%' OR pcolor LIKE '%".$key."%' OR warranty LIKE '%".$key."%' OR imagename LIKE '%".$key."%') //or(pKey LIKE '%".$key."%' OR pname LIKE '%".$key."%' OR mfg LIKE '%".$key."%' OR psize LIKE '%".$key."%' OR mrp LIKE '%".$key."%' OR saleprize LIKE '%".$key."%' OR pType LIKE '%".$key."%' OR pcolor LIKE '%".$key."%' OR warranty LIKE '%".$key."%' OR imagename LIKE '%".$key."%')"); $file_path = 'http://localhost/ProductDetails/images/'; while($row = mysql_fetch_assoc($result)) { print " Prod. ID" . $row['pKey'] . " NAME" . $row['pname'] . " SIZE" . $row['psize'] . " MRP" . $row['mrp'] . " COLOR" . $row['pcolor'] . " "; } mysql_close($con); } }?>


    • Heading

      #

Here is an example based on array_explode and appending query.

$keywords = "computers black color";
$array = explode(" ", $keywords);

$query = "select * from computers where";    
foreach($array as $key) {

        $query.= ' table_name_1 LIKE "%'.$key.'%"';
        $query.= ' or table_name_2 LIKE "%'.$key.'%"';
       //more tables here...
}

MAKE SURE that you escape values in query!!!