未找到任何行时,如何显示“未找到结果”消息?
while($row = $query->fetch(PDO::FETCH_ASSOC))
{
echo $row['name'];
}
$matched = false;
while($row = $query->fetch(PDO::FETCH_ASSOC))
{
$matched = true;
echo $row['name'];
}
if(!$matched)
echo "no any";
另一个..
$kittens = $query->fetchAll(PDO::FETCH_ASSOC))
if(!$kittens )
{
//Nada
}
else
{
foreach($kittens as $kitten)
{
}
}
只是一个替代解决方案,您可以添加一个 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";
}