mysqli查询中的数字返回数据但字符串不返回数据

时间:2015-07-14 17:13:46

标签: mysqli

所以我在mysqli中进行了多重过滤搜索,您可以使用文本框,集合ID或类型ID进行搜索。

问题是,当使用搜索文本框时,它会返回一个

Call to a member function fetch_object() on boolean

但是当我输入字符串或单个字母时,它只会返回该错误。当我输入一个数字,例如1或2时,它可以很好地工作并带来正确的结果。 谢谢


`
     $whereClauses = array();
   if (! empty($_POST['search'])) $whereClauses[] =       'tblfurniture.product_name ='.'%'.mysql_real_escape_string($_POST['search']).'%';
if (! empty($_POST['collection'])) $whereClauses[] ='tblfurniture.COLID='.mysql_real_escape_string($_POST['collection']);
if (! empty($_POST['type'])) $whereClauses[] = 'tblfurniture.TYPID='.mysql_real_escape_string($_POST['type']);

    $where = '';
     if (count($whereClauses) > 0) {
$where = 'WHERE '.implode(' AND ',$whereClauses);

} $results = $mysqli->query("SELECT * FROM tblfurniture JOIN tblcollection ON tblfurniture.COLID = tblcollection.COLID ".$where);

}

     else {
 $conn = mysqli_connect('localhost', 'root', '', 'hezefurniture');
     // Check connection
   if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
    }
  $results = $mysqli->query("SELECT * FROM tblfurniture 

JOIN tblcollection ON tblfurniture.COLID = tblcollection.COLID ORDER BY tblfurniture.product_id");

} while($row = $results->fetch_object()) {`

2 个答案:

答案 0 :(得分:0)

您的查询返回空值。 您似乎正在搜索文本框上执行错误查询, 使用mysqli_error()显示错误。

答案 1 :(得分:0)

解决:

 if (! empty($_POST['search'])) $whereClauses[] = 'tblfurniture.product_name ='.'%'.mysql_real_escape_string($_POST['search']).'%'; 

应该是

 if (! empty($_POST['search'])) $whereClauses[] = "tblfurniture.product_name ='%".mysql_real_escape_string($_POST['search'])."%'";