Php 如果';无结果';回响
我有一个基本的表单来过滤数据库。第一部分工作起来就像一场梦:Php 如果';无结果';回响,php,html,mysql,Php,Html,Mysql,我有一个基本的表单来过滤数据库。第一部分工作起来就像一场梦: $result=$dbh->prepare("SELECT * FROM performers WHERE accent='$txt_accent' AND hair='$txt_hair' AND gender='$txt_gender' AND location='$txt_location'"); $result->execute(); echo '<table><tr><th&g
$result=$dbh->prepare("SELECT * FROM performers WHERE accent='$txt_accent' AND hair='$txt_hair' AND gender='$txt_gender' AND location='$txt_location'");
$result->execute();
echo '<table><tr><th>id</th><th>name</th></tr>';
while($row=$result->fetch()) {
echo '<tr><td>'.$row['id'].'</td><td>'.$row['name'].'</td></tr>';
}
echo '</table>';
有什么想法吗?您可以在变量中使用
fetchAll
:
$rows = $result->fetchAll();
if (empty($rows)) {
echo 'No results';
} else {
foreach($rows as $row) {
// display results
}
}
这是不正确的->
如果($result=FALSE){echo'No results';}
如果(!$result){echo'No results';}或者如果($result==FALSE){echo'No results';}
不起作用。谢谢。哪种api用于连接和查询?Mysqli还是PDO?变量的值和来源是什么?您使用的是什么,pdo还是mysqli?“如果没有匹配项,我无法让php回显“无结果”-您必须这样做,以便您的查询确实失败才能获得结果。我真的不知道你为什么要这么做,或者这个问题到底是关于什么的,所以我会把这个投为“不清楚”。我要把这个插入哪里?它似乎根本不起作用。
$rows = $result->fetchAll();
if (empty($rows)) {
echo 'No results';
} else {
foreach($rows as $row) {
// display results
}
}