如果未找到任何行,如何显示“未找到结果”消息?
while($row = $query->fetch(PDO::FETCH_ASSOC))
{
echo $row['name'];
}
答案 0 :(得分:1)
$matched = false;
while($row = $query->fetch(PDO::FETCH_ASSOC))
{
$matched = true;
echo $row['name'];
}
if(!$matched)
echo "no any";
答案 1 :(得分:1)
另一个......
$kittens = $query->fetchAll(PDO::FETCH_ASSOC))
if(!$kittens )
{
//Nada
}
else
{
foreach($kittens as $kitten)
{
}
}
答案 2 :(得分:0)
只是一种替代解决方案,您可以添加if条件。
if ($row = $query->fetch(PDO::FETCH_ASSOC)) {
echo $row['name'];
while($row = $query->fetch(PDO::FETCH_ASSOC)) {
echo $row['name'];
}
} else {
echo "no results found";
}