CoryClark Posted January 13, 2010 Share Posted January 13, 2010 Okay, before I start: Mysql Version: 5.0.81, PHP Version: 5.2. What I want to do is display the number of unread messages from my `pms` table. By checking `touser` and `read`. The following code, works fine, but it doesn't check to the read field (unread posts are marked 0, while read ones are 1), instead it just displays the number of messages in the inbox, and it works perfectly: $toCount = "SELECT COUNT(*) FROM pms WHERE touser='$touser'"; $resCount = $database->query($toCount); $row = mysql_fetch_array($resCount, MYSQL_NUM); echo "You have <b>" . $row[0] . "</b> messages in your inbox."; However, when I try to check for unread messages, by using the following code: $toCount = "SELECT COUNT(*) FROM pms WHERE touser='$touser' AND read='0'"; $resCount = $database->query($toCount); $row = mysql_fetch_array($resCount, MYSQL_NUM); echo "You have <b>" . $row[0] . "</b> unread messages in your inbox."; I get the following error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource. I have searched the web countless times, and cannot find a solution to my problem. I am fairly new to Mysql and PHP and I don't know if I'm using AND in the wrong place or what...but any help or suggestions would be greatly appreciated. EDIT: $touser is simply declared as $touser = $session->username; further above... Quote Link to comment https://forums.phpfreaks.com/topic/188378-counting-unread-messages-from-database/ Share on other sites More sharing options...
mikesta707 Posted January 13, 2010 Share Posted January 13, 2010 seems like your query is failing. try adding or die(mysql_error()); on the same line as your query statement and see what it returns Quote Link to comment https://forums.phpfreaks.com/topic/188378-counting-unread-messages-from-database/#findComment-994472 Share on other sites More sharing options...
Andy-H Posted January 13, 2010 Share Posted January 13, 2010 Try changing your query to this: $toCount = "SELECT COUNT(*) FROM pms WHERE touser='$touser' AND `read`='0' "; Not 100% sure but I think read may be a MySQL keyword. Quote Link to comment https://forums.phpfreaks.com/topic/188378-counting-unread-messages-from-database/#findComment-994473 Share on other sites More sharing options...
CoryClark Posted January 13, 2010 Author Share Posted January 13, 2010 Wow it's funny how two simple ``s can mess with your day. Thank you Andy-H, that fixed everything, I guess it is a keyword. Quote Link to comment https://forums.phpfreaks.com/topic/188378-counting-unread-messages-from-database/#findComment-994476 Share on other sites More sharing options...
Andy-H Posted January 13, 2010 Share Posted January 13, 2010 LOL np, I've had that problem thousands of times myself. There's WAY too many reserved keywords in MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/188378-counting-unread-messages-from-database/#findComment-994481 Share on other sites More sharing options...
teynon Posted January 13, 2010 Share Posted January 13, 2010 Just my 2 cents, but I dont think that is MySQL's fault. You should shy away from using common words as names. READ, TABLE, SELECT, etc are all obvious things you should not name columns in your mysql database. May I even suggest column prefixes (IE: mail_user) Quote Link to comment https://forums.phpfreaks.com/topic/188378-counting-unread-messages-from-database/#findComment-994486 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.