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. Link to comment https://forums.phpfreaks.com/topic/116354-determining-if-table-has-no-record/ 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"; } } ?> Link to comment https://forums.phpfreaks.com/topic/116354-determining-if-table-has-no-record/#findComment-598276 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 } Link to comment https://forums.phpfreaks.com/topic/116354-determining-if-table-has-no-record/#findComment-598336 Share on other sites More sharing options...
unkwntech Posted July 24, 2008 Share Posted July 24, 2008 I'll Second Barand on this. Link to comment https://forums.phpfreaks.com/topic/116354-determining-if-table-has-no-record/#findComment-598338 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. Link to comment https://forums.phpfreaks.com/topic/116354-determining-if-table-has-no-record/#findComment-598350 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. Link to comment https://forums.phpfreaks.com/topic/116354-determining-if-table-has-no-record/#findComment-598768 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. Link to comment https://forums.phpfreaks.com/topic/116354-determining-if-table-has-no-record/#findComment-598871 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. Link to comment https://forums.phpfreaks.com/topic/116354-determining-if-table-has-no-record/#findComment-598874 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 Link to comment https://forums.phpfreaks.com/topic/116354-determining-if-table-has-no-record/#findComment-599011 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.