我想創建一個搜索功能,用戶可以在文本字段中輸入兩個單詞,它將分割單詞並構建MySQL查詢。使用多個字的PHP搜索
這是我到目前爲止。
$search = mysql_real_escape_string($_POST['text_field']);
$search = explode(" ", $search);
foreach($search as $word)
{
$where = "";
$where .= "product_code LIKE '%". $word ."%'";
$where .= "OR description LIKE '%". $word ."%'";
$query = "SELECT * FROM customers WHERE $where";
$result = mysql_query($query) or die();
if(mysql_num_rows($result))
{
while($row = mysql_fetch_assoc($result))
{
$customer['value'] = $row['id'];
$customer['label'] = "{$row['id']}, {$row['name']} {$row['age']}";
$matches[] = $customer;
}
}
else
{
$customer['value'] = "";
$customer['label'] = "No matches found.";
$matches[] = $customer;
}
}
$matches = array_slice($matches, 0, 5); //return only 5 results
它構造並運行查詢,但返回有趣的結果。
任何幫助,將不勝感激。
請解釋什麼有趣的結果 –
任何你不能使用全文索引的原因嗎?使用'like'%...%''排除索引的使用,並且性能在大型桌面上會很糟糕。什麼是「有趣的結果」?他們讓你發笑嗎?同樣,你應該逃避單個單詞,而不是字符串在你操縱它之前 –