Hi
can anyone please let me know , how to check if table is empty or not? I am using php5 and mysql.
Thanks in advance.
:-)
You can try counting the number of values in the table, if this is 0 then there are no entries.
Your mysql_query should contain something similar to this:
SELECT count(*) FROM `table_name`
You can try counting the number of values in the table, if this is 0 then there are no entries.
Your mysql_query should contain something similar to this:
SELECT count(*) FROM `table_name`
here is how i did this.
$query=mysql_query("select entryName from tableName");
$row=mysql_fetch_array($query);
if ($row!="")
{
echo "table not empty ";
}
else
{
echo "table empty ";
}
$query = "SELECT * FROM symbols";
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
echo "Table is not Empty";
}
else echo "Table is Empty";
The above is the simple logic when you used in php or else follow the count(*) in the sql query.
If all you want to know is if the table is empty, then Will's approach is better because his query will return 0 if its empty or tell you how many entries you actually have.
On the other hand, if you are going to print some data from the database then Rajarajan's method would work best as you could do the output right in the section where he says "table is not empty".
If all you want to know is if the table is empty, then Will's approach is better because his query will return 0 if its empty or tell you how many entries you actually have.
On the other hand, if you are going to print some data from the database then Rajarajan's method would work best as you could do the output right in the section where he says "table is not empty".
I executed Will's query as
$q=mysql_query("SELECT count(*) FROM workforce");
$q2=mysql_num_rows($q);
echo $q2;
and output was 1 when there were 5 rows in the table as well as there were no records or table was empty. so how to find this 0 and 1
because when i write like:
$q=mysql_query("SELECT count(*) FROM workforce");
echo $q;
it gives Resource#4 in each case.
I executed Will's query as
$q=mysql_query("SELECT count(*) FROM workforce");
$q2=mysql_num_rows($q);echo $q2;
and output was 1 when there were 5 rows in the table as well as there were no records or table was empty. so how to find this 0 and 1
because when i write like:
$q=mysql_query("SELECT count(*) FROM workforce");
echo $q;it gives Resource#4 in each case.
Rajarajan's method worked well.
Thanks
Welcome, Mark as solved if it is solved.
try this:
$x = 'SELECT COUNT(*) FROM workforce';
$result = mysql_query($x) or die(mysql_error());
$total_rows = mysql_fetch_row($result);
echo $total_rows[0];
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.