我有一个简单的搜索栏,可以用来搜索客户的姓名、电话号码、地址、注释或日期。我从我的 customer
表中搜索数据,该表包含以下字段:id
、name
、telno
、地址
、注释
和日期
。我的问题是:为什么即使我没有收到任何错误消息,也无法获得搜索结果?
这是我的代码:
if (isset($_POST['search']) && !empty($_POST['search'])){
$db =new mysqli('localhost', 'root', '1234' , 'trying');
if($db->connect_errno) die ('Connect Error:' . $db->connect_errno);
$stmt = $db ->prepare("select * from customer where name or note or date or telno like ?");
if($stmt === FALSE) die("Query Error". $db ->error);
$ara ='%'.$_POST['search'].'%';
$stmt->bind_param("s", $ara);
$stmt->execute();
$res =$stmt->get_result();
if($res -> num_rows <1) die("can't found !");
while ($row = $res->fetch_array())
{
echo $row['name']. '<br/>';
echo $row['telno']. '<br/>';
echo $row['note']. '<br/>';
echo $row['adress']. '<br/>';
echo $row['date']. '<br/>';
}
}
最佳答案
您的查询完全错误。您无法像现在这样针对单个参数测试多个值:
where name or note or date or telno like ?
^^ ^^ ^^
这被执行为
where (some value) or (some value) or (some value) or (some value LIKE ?)
where (t/f) or (t/f) or (t/f) or (some value like ?)
where (t/f) or (some value like ?)
您必须对每个值进行 like
测试:
where (name LIKE ?) or (note LIKE ?) or (date LIKE ?) etc...
关于php - 为什么我无法获取搜索结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32997365/