0

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.
:-)

4
Contributors
8
Replies
15
Views
7 Years
Discussion Span
Last Post by ppetree
0

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`
0

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 ";
}

0
$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.

0

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".

0

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.

0

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

0

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];
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.