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"; } Quote Link to comment 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. Quote Link to comment 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) { Quote Link to comment 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"; } ?> Quote Link to comment 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 ) Quote Link to comment 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. } ?> Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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"; } Quote Link to comment 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"; } } ?> Quote Link to comment 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 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.