ballhogjoni Posted June 28, 2007 Share Posted June 28, 2007 I am connecting to my DB table and then exporting that table info to excel. My question is, is there a function that will let me know if there are no records in my table? Something like $no_records = mysql_num_rows($sql=0) if ($no_records=0) { echo "You have No Records"; } Link to comment https://forums.phpfreaks.com/topic/57573-solved-what-would-be-the-code-for-this/ Share on other sites More sharing options...
conker87 Posted June 28, 2007 Share Posted June 28, 2007 <?php $no_records = mysql_num_rows($sql) if ($no_records==0) { echo "You have No Records"; } ?> Try that. Link to comment https://forums.phpfreaks.com/topic/57573-solved-what-would-be-the-code-for-this/#findComment-284938 Share on other sites More sharing options...
ballhogjoni Posted June 28, 2007 Author Share Posted June 28, 2007 Lol gotta get my operators correct. I get a syntax error, unexpected T_IF on line 43 which is if ($no_records==0) { Link to comment https://forums.phpfreaks.com/topic/57573-solved-what-would-be-the-code-for-this/#findComment-284943 Share on other sites More sharing options...
bbaker Posted June 28, 2007 Share Posted June 28, 2007 <?php $no_records = mysql_num_rows($sql); //<== missing ; if ($no_records==0) { echo "You have No Records"; } ?> Link to comment https://forums.phpfreaks.com/topic/57573-solved-what-would-be-the-code-for-this/#findComment-284946 Share on other sites More sharing options...
clanstyles Posted June 28, 2007 Share Posted June 28, 2007 $no_records = mysql_num_rows($sql) change to: $no_records = mysql_num_rows($sql); ( FORGOT THE ; AT THE END ) Link to comment https://forums.phpfreaks.com/topic/57573-solved-what-would-be-the-code-for-this/#findComment-284947 Share on other sites More sharing options...
trq Posted June 28, 2007 Share Posted June 28, 2007 All you really need is... <?php if (!mysql_num_rows($sql)) { // no records. } ?> Link to comment https://forums.phpfreaks.com/topic/57573-solved-what-would-be-the-code-for-this/#findComment-284948 Share on other sites More sharing options...
ballhogjoni Posted June 28, 2007 Author Share Posted June 28, 2007 This is the code I have: if (!mysql_num_rows($sql)) { echo "You have No Records"; } But I get this warning and the code still executes: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource on line 16 You have No Records Link to comment https://forums.phpfreaks.com/topic/57573-solved-what-would-be-the-code-for-this/#findComment-284960 Share on other sites More sharing options...
conker87 Posted June 28, 2007 Share Posted June 28, 2007 All you really need is... <?php if (!mysql_num_rows($sql)) { // no records. } ?> Hmm! I shall be using this from now on. Ta very much there thorpe. Link to comment https://forums.phpfreaks.com/topic/57573-solved-what-would-be-the-code-for-this/#findComment-284961 Share on other sites More sharing options...
trq Posted June 28, 2007 Share Posted June 28, 2007 Then your query that produces the $sql (poor choice for variable name by the way) result is failing and your not checing it before using it. We need to see more code. Specifically, the query that returns $sql. Link to comment https://forums.phpfreaks.com/topic/57573-solved-what-would-be-the-code-for-this/#findComment-284975 Share on other sites More sharing options...
ballhogjoni Posted June 28, 2007 Author Share Posted June 28, 2007 It was a script someone else wrote and I dint bother changing variable name. $sql = "Select * from contact_n_questions"; if (!mysql_num_rows($sql)) { echo "You have No Records"; } Link to comment https://forums.phpfreaks.com/topic/57573-solved-what-would-be-the-code-for-this/#findComment-284982 Share on other sites More sharing options...
trq Posted June 28, 2007 Share Posted June 28, 2007 You need to actually execute the query. eg; <?php $sql = "Select * from contact_n_questions"; if ($result = mysql_query($sql)) { if (!mysql_num_rows($result)) { echo "You have No Records"; } } ?> Link to comment https://forums.phpfreaks.com/topic/57573-solved-what-would-be-the-code-for-this/#findComment-284984 Share on other sites More sharing options...
ballhogjoni Posted June 28, 2007 Author Share Posted June 28, 2007 Thank everyone. You helped out a bunch ;D Link to comment https://forums.phpfreaks.com/topic/57573-solved-what-would-be-the-code-for-this/#findComment-284994 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.