shorty3 Posted August 23, 2010 Share Posted August 23, 2010 right this is from my database Database: inbox right i did this code: <?php $totalmess2 = mysql_num_rows(mysql_query("SELECT read='0' FROM inbox")); ?> then <?php echo "$totalmess2"; ?> ive got read in my inbox which just shows whether some one as read the message or not 1=read 0=unread i want to know the total unread message there is in my inbox database whats going wrong Quote Link to comment Share on other sites More sharing options...
trq Posted August 23, 2010 Share Posted August 23, 2010 So many things wrong with this code. Firstly, executing mysql_query() within mysql_num_rows() like that is ridiculous. it gives you absolutely no opportunity to check your query for success before attempting to use its result. Speaking of which, your query is failing because its syntax is incorrect. You might try..... SELECT id FROM inbox WHERE read = '0' So, that would be.... if ($result = mysql_query("SELECT id FROM inbox WHERE read = '0'")) { echo mysql_num_rows($result); } Now, considering you only want a count and not the actual data, this would be even better. if ($result = mysql_query("SELECT COUNT(id) AS cnt FROM inbox WHERE read = '0'")) { if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); echo $row['cnt']; } } Quote Link to comment Share on other sites More sharing options...
shorty3 Posted August 23, 2010 Author Share Posted August 23, 2010 nope it didn`t work ? Quote Link to comment Share on other sites More sharing options...
trq Posted August 23, 2010 Share Posted August 23, 2010 nope it didn`t work ? Thats helpful. Try debugging it. if ($result = mysql_query("SELECT COUNT(id) AS cnt FROM inbox WHERE read = '0'")) { if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); echo $row['cnt']; } else { // no records found. } } else { trigger_error(mysql_error()); } 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.