coder9 Posted July 24, 2008 Share Posted July 24, 2008 what commands i need to determine if the table has no record or empty? thank you. Quote Link to comment Share on other sites More sharing options...
trq Posted July 24, 2008 Share Posted July 24, 2008 <?php // connect to db. $sql = "SELECT foo FROM tbl"; if ($result = mysql_query($sql)) { if (!mysql_num_rows($result)) { echo "Table is empty"; } } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted July 24, 2008 Share Posted July 24, 2008 More efficient to use COUNT $res = mysql_query("SELECT COUNT(*) FROM table"); if (mysql_result($res,0)==0) { // empty } Quote Link to comment Share on other sites More sharing options...
unkwntech Posted July 24, 2008 Share Posted July 24, 2008 I'll Second Barand on this. Quote Link to comment Share on other sites More sharing options...
Third_Degree Posted July 24, 2008 Share Posted July 24, 2008 The battle of a 13,574 posts member and a 14,131 posts member But I, also, will go with Barand. Quote Link to comment Share on other sites More sharing options...
trq Posted July 24, 2008 Share Posted July 24, 2008 I'm not going to argue, though I don't really see one being more efficient than the other. The both require the execution of one query and both use two function calls. Really wouldn't be much in it. Quote Link to comment Share on other sites More sharing options...
Third_Degree Posted July 24, 2008 Share Posted July 24, 2008 I was too lazy to consult the php source on this one, but if I recall, mysql_num_rows() would actually be loading a new string/array engine to actually count the number of records, but I could be woefully wrong (I guess it would do this in mysql too, but as I said, I checked neither source). I guess the only way to test it would be a speed test of each script. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 24, 2008 Share Posted July 24, 2008 Count(*) only ever returns a single field no matter how many recs are in the file. If there were 1000 recs in the file, SELECT foo pulls a 1000 foos down just so you can call num_rows(). "SELECT COUNT(*) FROM tbl" is optimised so it doesn't even need to read the table, it just looks it up in its internal tables. Quote Link to comment Share on other sites More sharing options...
trq Posted July 24, 2008 Share Posted July 24, 2008 Count(*) only ever returns a single field no matter how many recs are in the file. If there were 1000 recs in the file, SELECT foo pulls a 1000 foos down just so you can call num_rows(). "SELECT COUNT(*) FROM tbl" is optimised so it doesn't even need to read the table, it just looks it up in its internal tables. That makes sense, you win Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.