I have read several of these posts on the site, but still can't find the answer to my problems. I have a while loop where for every entry in the database the table populates. Although if the table in the database is empty, I want it to display a messaged instead. Any ideas of what is wrong here? (aware of the deprecated tags)
$result = mysql_query("SELECT * FROM blog");
while($row = mysql_fetch_array($result))
{
if(count($row) === 0)
{
echo 'No Data';
}
<table code>
}
Use mysql_num_rows
for counting rows from DB.
<?php
if (mysql_num_rows($result) > 0) {
echo '<table>';
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>...</tr>';
}
echo '</table>';
} else {
echo 'No result found';
}
?>
EDIT: updated code for table.
$result = mysql_query("SELECT * FROM blog");
if(mysql_num_rows($result) === 0) {
echo 'No Data';
} else {
while($row = mysql_fetch_array($result)) {
// code
}
}
use mysql_num_rows()
to get count of query result rows
$rows = mysql_num_rows($result);
if($rows > 0) {
// do your stuff
}
Use mysql_num_rows
for counting number of rows are returned in query.
Try this. mysql_num_rows will check the records in database. On true condition it will allow to execute the while loop other wise else condition will execute.
$result = mysql_query("SELECT * FROM blog");
if(mysql_num_rows($result) > 0) {
echo 'No Result Found';
} else {
while($row = mysql_fetch_array($result)) {
// Here your Data
}
}
If you want to display when no record found then
$result = mysql_query("SELECT * FROM blog");
while($row = mysql_fetch_array($result))
{
if(count($row) == 0 || count($row) < 1)
{
echo 'No Data';
}
else
{
//print records
}
}