hankknight
asked on
Test is mySQL table is completely empty or not
Hello,
I use the following to add content to a mySQL table:
$results=mysql_query("INSE RT INTO book(page,chapter,priority ,text,time ) VALUES('".mysql_real_escap e_string($ page)."',' ".mysql_re al_escape_ string($ch apterNumbe r)."','".m ysql_real_ escape_str ing($prior ity)."','" .mysql_rea l_escape_s tring (trim($text))."',".time(). ")") or die (mysql_error());
But BEFORE I add anything I want to confirm if there is ANYTHING already in the table.
What is the best way to test is a mySQL table is not completely empty?
I use the following to add content to a mySQL table:
$results=mysql_query("INSE
But BEFORE I add anything I want to confirm if there is ANYTHING already in the table.
What is the best way to test is a mySQL table is not completely empty?
SELECT * FROM BOOK would return all the rows in the table if it wasn't empty. This would be quite a bit of unneeded system resources.
$result = mysql_query("SELECT COUNT(*) as num_rows FROM book");
$count = mysql_fetch_assoc($result) ;
if($count['num_rows'] > 0){
echo 'not empty';
}
$result = mysql_query("SELECT COUNT(*) as num_rows FROM book");
$count = mysql_fetch_assoc($result)
if($count['num_rows'] > 0){
echo 'not empty';
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you.
I will use this:
I will use this:
$result = mysql_query("SELECT COUNT(*) as rowcheck FROM book LIMIT 1");
$count = mysql_fetch_assoc($result);
if($count['rowcheck'] > 0){
echo 'not empty';
}
if ( mysql_num_rows($result) > 0 )
echo "Not Empty";