When no rows are found, how do I show a "no results found" message?
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";
Just an alternative solution, you could add a if condition.
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";
}
Another one..
$kittens = $query->fetchAll(PDO::FETCH_ASSOC))
if(!$kittens )
{
//Nada
}
else
{
foreach($kittens as $kitten)
{
}
}