2013-02-13 77 views
1

幫助是必需的。 我正在準備一個搜索表單。需要幫助在選擇查詢 是否有可能我可以搜索「建設」字在我的表中的所有列 下面是我試過的查詢。mysql select查詢搜索多列中的一個詞

$search = $_POST['search']; 
$sql = mysql_query("SELECT * FROM property WHERE posthead,requestto,ptype,requestby,owner,bed,bath,price,sqft,descp = '".$search."'"); 
$result = (mysql_fetch_array($sql)); 

echo "Post Head : ". $result['posthead']."<br />"; 
echo "Request To : ". $result['requestto']."<br />"; 
echo "Type : ". $result['ptype']."<br />"; 
echo "Request By : ". $result['requestby']."<br />"; 
echo "Owner : ". $result['owner']."<br />"; 
echo "Bed : ". $result['bed']."<br />"; 
echo "Bath : ". $result['bath']."<br />"; 
echo "Price : ". $result['price']."<br />"; 
echo "Sq ft. : ". $result['sqft']."<br />"; 
echo "Description : ". $result['descp']."<br />"; 
+0

是的,閱讀:http://dev.mysql.com/doc/refman/5.0/en/where-optimizations.html – Jon 2013-02-13 04:48:54

回答

0

嘗試SELECT * FROM property WHERE posthead = $search OR requestto = $search ...

+0

多數民衆贊成在很大程度上。它爲我工作。感謝afarazit :) – 2013-02-13 05:05:49

2
$sql = mysql_query("SELECT * FROM property WHERE posthead = '".$search."' OR requestto= '".$search."' OR ptype= '".$search."' OR requestby = '".$search."' OR owner= '".$search."' OR bed = '".$search."' OR bath= '".$search."' OR price= '".$search."' OR sqft,descp = '".$search."'"); 

,改變$result = (mysql_fetch_array($sql));$result = (mysql_fetch_assoc($sql));

注:

  1. 查詢很容易受到SQL注入
  2. mysql_ *功能已被棄用使用mysqli_ *或PDO
0

因爲要創建您可能希望能夠找到的部分匹配,而不是確切的詞記錄的搜索形式。爲了做到這一點,你可以使用LIKE

$q = "SELECT * FROM property 
WHERE posthead LIKE '%$search%' OR 
     requestto LIKE '%$search%' OR 
     ptype  LIKE '%$search%' OR 
     requestby LIKE '%$search%' OR 
     owner  LIKE '%$search%' OR 
     bed  LIKE '%$search%' OR 
     bath  LIKE '%$search%' OR 
     price  LIKE '%$search%' OR 
     sqft  LIKE '%$search%' OR 
     descp  LIKE '%$search%'"; 

$sql = mysql_query($q); 
1

試試這個:

更好的方法是使用"CONCAT"爲列:

$ SQL =請求mysql_query(「SELECT * FROM property WHERE CONCAT(posthead,requestto,ptype,requestby,owner,bed,bath,price,sqft,descp) LIKE「%。$ search。」%'「);

OR將工作,但CONCAT可以減少查詢量。