1

需要帮助。我正在准备一个搜索表格。在选择查询中需要帮助是否有可能我可以在我的表的所有列中搜索“建筑”字以下是我尝试过的查询。

$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 />";
4

4 回答 4

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
于 2013-02-13T04:48:50.023 回答
1

试试这个:

更好的是"CONCAT"用于列:

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

OR将起作用,但CONCAT可以减少查询大小。

于 2014-12-27T20:19:50.183 回答
0

尝试SELECT * FROM property WHERE posthead = $search OR requestto = $search ...

于 2013-02-13T04:49:47.933 回答
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);
于 2013-02-13T05:08:20.260 回答