Returns MySQL result if 0 rows are returned
I have this PHP code:
$query = "SELECT name, COUNT(message) FROM guestbook_message WHERE name='".$req_user_info['username']."' GROUP BY name";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo "Messages posted: ". $row['COUNT(message)'] ."";
echo "<br />";
}
The number of comments submitted by the user will be displayed.
How can I return a value if there are no messages sent by this user? Nothing is currently displayed. But I want it to show "Messages sent: 0"
Any ideas?
Check the number of results returned as a result using mysql_num_rows.
$query = "SELECT name, COUNT(message) FROM guestbook_message WHERE name='".$req_user_info['username']."' GROUP BY name";
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result) > 0)
while($row = mysql_fetch_array($result))
{
echo "Messages posted: ". $row['COUNT(message)'] ."";
echo "<br />";
}
else
echo "NO Messages posted. <br />";
, ?
$stmt = $con->prepare("SELECT * FROM subjects $qury");
$stmt->execute();
$row = $stmt->fetchAll();
if ($row > 0 ){
foreach($row as $row) {
echo "<tr>";
echo "<th scope='row'>" . $row['titleID'] . "</th>";
echo "<td>" . $row['nickname'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td class='w-50 p-3'>" . $row['story'] . "</td>";
echo "<td>" . $row['datetime'] . "</td>";
echo "<td>
<a href='story.php?do=edit&t_id=" . $row['titleID'] . "' class='btn btn-outline-success btn-sm'><i class='fa fa-edit'></i></a>
<a href='story.php?do=delete&t_id=" . $row['titleID'] . "' class='btn btn-outline-danger btn-sm confirm'><i class='fa fa-edit'></i></a> ";
if($row['published'] == 0){
echo "<a href='story.php?do=publish&t_id=" . $row['titleID'] . "' class='btn btn-outline-success btn-sm'><i class='far fa-check-circle'></i></a>";
}
echo "</td>";
echo "</tr>";
}
}else {
echo "NO Messages posted. <br />";
}