runnerjp Posted June 5, 2008 Share Posted June 5, 2008 currently i have my code like this <?php session_start(); include '../settings.php'; $user= get_username($_SESSION['user_id']); $query = "SELECT received FROM messages WHERE reciever = '$user' and received = '1' "; if ($result = mysql_query($query)){ if (mysql_num_rows($result)) { $array = mysql_fetch_assoc($result); $messages = echo $messages; } } ?> but im not sure how i would count the number of messages recieved = 1 and display the number Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/ Share on other sites More sharing options...
Daniel0 Posted June 5, 2008 Share Posted June 5, 2008 Try SELECT COUNT(*) AS received FROM messages WHERE receiver = '$user' AND received = 1; Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558245 Share on other sites More sharing options...
runnerjp Posted June 5, 2008 Author Share Posted June 5, 2008 what would i echo ? Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558246 Share on other sites More sharing options...
Daniel0 Posted June 5, 2008 Share Posted June 5, 2008 $query = "SELECT COUNT(*) AS received FROM messages WHERE receiver = '$user' AND received = 1"; if (($result = mysql_query($query)) && mysql_num_rows($result)){ $data = mysql_fetch_assoc($result); echo $data['received']; } Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558253 Share on other sites More sharing options...
runnerjp Posted June 5, 2008 Author Share Posted June 5, 2008 i echoed my query and its correct but its displaying no results Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558259 Share on other sites More sharing options...
runnerjp Posted June 5, 2008 Author Share Posted June 5, 2008 sorry i spelt somethign wrong doh lol ty that works great Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558262 Share on other sites More sharing options...
runnerjp Posted June 5, 2008 Author Share Posted June 5, 2008 ok did a bit of editing <? session_start(); include '../settings.php'; $user= get_username($_SESSION['user_id']); $query = "SELECT COUNT(*) AS recieved FROM messages WHERE reciever = '$user' AND recieved = 1"; if (($result = mysql_query($query)) && mysql_num_rows($result)){ $data = mysql_fetch_assoc($result); $messages = $data['recieved']; if($data['recieved'] < 1) { echo "you have $messages message"; } //There are no errors so far which means the form is completely filled out else { echo 'no messages'; } } ?> have i done if($data['recieved'] < 1) right as it give me 0 even though i have 2 messages Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558263 Share on other sites More sharing options...
trq Posted June 5, 2008 Share Posted June 5, 2008 That code will echo 'you have 0 message' if you have no messages and 'no messages' if you have some. Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558266 Share on other sites More sharing options...
runnerjp Posted June 5, 2008 Author Share Posted June 5, 2008 humm but i swapped it around <? session_start(); include '../settings.php'; $user= get_username($_SESSION['user_id']); $query = "SELECT COUNT(*) AS recieved FROM messages WHERE reciever = '$user' AND recieved = 1"; if (($result = mysql_query($query)) && mysql_num_rows($result)){ $data = mysql_fetch_assoc($result); $messages = $data['recieved']; if($data['recieved'] < 1) { echo 'no messages'; } else { echo "you have $messages message"; } } ?> and i get you have no messages lol still even tho i have 2! Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558268 Share on other sites More sharing options...
runnerjp Posted June 5, 2008 Author Share Posted June 5, 2008 intrestingly i did this again.. <?$query = "SELECT COUNT(*) AS recieved FROM messages WHERE reciever = 'Admin' AND recieved = 1"; if (($result = mysql_query($query)) && mysql_num_rows($result)){ $data = mysql_fetch_assoc($result); echo $data['recieved']; }?> and it also shows 0 so this is the problem.... if only 1 colum in my db is set to 1 it shows 1 if more then 1 it shows 0 :S Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558272 Share on other sites More sharing options...
trq Posted June 5, 2008 Share Posted June 5, 2008 Just noticed you already have a field names 'recieved', you'll need to change the alias to something else. <?php session_start(); include '../settings.php'; $user= get_username($_SESSION['user_id']); $query = "SELECT COUNT(*) AS c FROM messages WHERE reciever = '$user' AND recieved = 1"; if (($result = mysql_query($query)) && mysql_num_rows($result)){ $data = mysql_fetch_assoc($result); $messages = $data['c']; if ($data['recieved'] < 1) { echo 'no messages'; } else { echo "you have $messages message"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558306 Share on other sites More sharing options...
runnerjp Posted June 5, 2008 Author Share Posted June 5, 2008 but that echos no messages even tho still there are 2 lol Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558319 Share on other sites More sharing options...
jonsjava Posted June 5, 2008 Share Posted June 5, 2008 he forgot to change a variable: <?php session_start(); include '../settings.php'; $user= get_username($_SESSION['user_id']); $query = "SELECT COUNT(*) AS c FROM messages WHERE reciever = '$user' AND recieved = 1"; if (($result = mysql_query($query)) && mysql_num_rows($result)){ $data = mysql_fetch_assoc($result); $messages = $data['c']; if ($data['c'] < 1) { echo 'no messages'; } else { echo "you have $messages message"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558322 Share on other sites More sharing options...
runnerjp Posted June 5, 2008 Author Share Posted June 5, 2008 humm no that still does not work i just keep getting no messages Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558330 Share on other sites More sharing options...
jonsjava Posted June 5, 2008 Share Posted June 5, 2008 this is just a test script, to see what you get: <?php session_start(); include '../settings.php'; $user= get_username($_SESSION['user_id']); $query = "SELECT COUNT(*) AS c FROM messages WHERE reciever = '$user' AND recieved = 1"; if (($result = mysql_query($query)) && mysql_num_rows($result)){ $data = mysql_fetch_assoc($result); $messages = $data['c']; print $messages; exit(); if ($data['c'] < 1) { echo 'no messages'; } else { echo "you have $messages message"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558331 Share on other sites More sharing options...
jonsjava Posted June 5, 2008 Share Posted June 5, 2008 posting again. having 666 as my post count creaped me out Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558333 Share on other sites More sharing options...
runnerjp Posted June 5, 2008 Author Share Posted June 5, 2008 ok update it says 1 message if i have 1 message but if i have more then 1 it says no messages Full Texts id reciever sender subject message recieved time Edit Delete 2 Admin Admin test test 1 1212507884 Edit Delete 4 Admin Admin testing hey 1 1212507884 thats what my table looks like edit : what you sent me it outputted 0 Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558336 Share on other sites More sharing options...
jonsjava Posted June 5, 2008 Share Posted June 5, 2008 what does this script do for you (I know it's a step backwards, but....) <?php session_start(); include '../settings.php'; $user= get_username($_SESSION['user_id']); $query = "SELECT * FROM messages WHERE reciever = '$user' AND recieved = 1"; $result = mysql_query($query); $no_of_msgs = mysql_num_rows($result); if ($no_of_msgs > 0){ $data = mysql_fetch_assoc($result); echo "you have $no_of_msgs message"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558345 Share on other sites More sharing options...
runnerjp Posted June 5, 2008 Author Share Posted June 5, 2008 gives me a blank page Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558357 Share on other sites More sharing options...
jonsjava Posted June 5, 2008 Share Posted June 5, 2008 slightly modified version (forgot the else) <?php session_start(); include '../settings.php'; $user= get_username($_SESSION['user_id']); $query = "SELECT * FROM messages WHERE reciever = '$user' AND recieved = 1"; $result = mysql_query($query); $no_of_msgs = mysql_num_rows($result); if ($no_of_msgs != 0){ $data = mysql_fetch_assoc($result); echo "you have $no_of_msgs message"; } else{ print "No messages found"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558360 Share on other sites More sharing options...
runnerjp Posted June 5, 2008 Author Share Posted June 5, 2008 i get no messages found Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558366 Share on other sites More sharing options...
fanfavorite Posted June 5, 2008 Share Posted June 5, 2008 Just a note received is spelt wrong in the query, so please make sure it is spelt wrong in your database. Also, your query was looking for recieved = 1, so if it was more than 1, it would not return a result. I have changed it to != 0. Give this a try: <?php session_start(); include '../settings.php'; $user= get_username($_SESSION['user_id']); $query = "SELECT * FROM messages WHERE reciever = '$user' AND recieved != 0"; $result = mysql_query($query); $no_of_msgs = mysql_num_rows($result); if ($no_of_msgs != 0){ $data = mysql_fetch_assoc($result); echo "you have $no_of_msgs message"; } else{ print "No messages found"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558370 Share on other sites More sharing options...
jonsjava Posted June 5, 2008 Share Posted June 5, 2008 If fanfavorite's advice doesn't help, and you get the same result, that usually means that it's returnig a "0" on the num_rows result. Not much we can do besides have you print out the query you are running: <?php session_start(); include '../settings.php'; $user= get_username($_SESSION['user_id']); $query = "SELECT * FROM messages WHERE reciever = '$user' AND recieved = 1"; print $query; exit(); when you run that, do you get the expected user in the $user field? Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558371 Share on other sites More sharing options...
runnerjp Posted June 5, 2008 Author Share Posted June 5, 2008 ahh fans idea worked... i didnt take into fact that it =1 would mean only 1 result... thanks for sticking with me through this 1 and really drilling to get the idea.. learned alot!! i would like to know so i undertsand it fully the use of != 0 Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558377 Share on other sites More sharing options...
fanfavorite Posted June 5, 2008 Share Posted June 5, 2008 != means NOT EQUAL TO. Which means that if recieved equals anything other than 0, it will return true. With recieved = 1, it would only return true if there was 1 message. I hope I didn't confuse you are all. Quote Link to comment https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/#findComment-558385 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.