andrew_biggart Posted March 10, 2009 Share Posted March 10, 2009 Im trying to count the numbe of messages a user has and i can get it to count all the messages but when i try and add the where statement it doesnt display the number grrrrr. Im using the follwoing code. <?php include("config_contact.php"); $username=$_SESSION['myusername']; // Retrieve data from database $sql = "SELECT count(Message_user) as mescount WHERE Message_user=$username FROM User_messagesT "; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); echo $row['mescount']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/148779-solved-counting-the-number-of-messages-a-user-has-and-displaying-the-number/ Share on other sites More sharing options...
.josh Posted March 10, 2009 Share Posted March 10, 2009 put quotes around your variable in your query Quote Link to comment https://forums.phpfreaks.com/topic/148779-solved-counting-the-number-of-messages-a-user-has-and-displaying-the-number/#findComment-781203 Share on other sites More sharing options...
waterssaz Posted March 10, 2009 Share Posted March 10, 2009 Your syntax is incorrect for your query. The WHERE clause comes after the FROM Quote Link to comment https://forums.phpfreaks.com/topic/148779-solved-counting-the-number-of-messages-a-user-has-and-displaying-the-number/#findComment-781206 Share on other sites More sharing options...
andrew_biggart Posted March 10, 2009 Author Share Posted March 10, 2009 is this right now? quotes on which query? <?php include("config_contact.php"); $username=$_SESSION['myusername']; // Retrieve data from database $sql = "SELECT count(Message_user) as mescount FROM User_messagesT WHERE Message_user=$username "; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); echo $row['mescount']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/148779-solved-counting-the-number-of-messages-a-user-has-and-displaying-the-number/#findComment-781213 Share on other sites More sharing options...
waterssaz Posted March 10, 2009 Share Posted March 10, 2009 $sql = "SELECT count(Message_user) as mescount FROM User_messagesT WHERE Message_user='$username' "; Notice quotes around the variable $username above Quote Link to comment https://forums.phpfreaks.com/topic/148779-solved-counting-the-number-of-messages-a-user-has-and-displaying-the-number/#findComment-781220 Share on other sites More sharing options...
andrew_biggart Posted March 10, 2009 Author Share Posted March 10, 2009 lol as if i forgot that, thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/148779-solved-counting-the-number-of-messages-a-user-has-and-displaying-the-number/#findComment-781223 Share on other sites More sharing options...
andrew_biggart Posted March 10, 2009 Author Share Posted March 10, 2009 one last thing sorry im trying to get the number to be displayed in brackets ie messages (5) nd when i try and put the brackets in the echo is fucks up so ive done it this way. With them before and after the php. But the problem is they show up even if there is no messages so how do i dd them to the echo successfully so they only show up when there is a number to display? (<?php include("config_contact.php"); $username=$_SESSION['myusername']; // Retrieve data from database $sql = "SELECT count(Message_user) as mescount FROM User_messagesT WHERE Message_user='$username' "; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); echo $row['mescount']; ?>) Quote Link to comment https://forums.phpfreaks.com/topic/148779-solved-counting-the-number-of-messages-a-user-has-and-displaying-the-number/#findComment-781231 Share on other sites More sharing options...
lonewolf217 Posted March 10, 2009 Share Posted March 10, 2009 <?php if($row['mescount'] != 0) { echo "(" . $row['mescount'] . ")"; } ?> assuming that mescount is a numeric value type Quote Link to comment https://forums.phpfreaks.com/topic/148779-solved-counting-the-number-of-messages-a-user-has-and-displaying-the-number/#findComment-781236 Share on other sites More sharing options...
waterssaz Posted March 10, 2009 Share Posted March 10, 2009 Count the number of rows returned by the query using mysql_num_rows(). Then you can use an If statement to decide wether or not to display the brackets around the result, depending on if mysql_num_rows returns more than 1 row. Quote Link to comment https://forums.phpfreaks.com/topic/148779-solved-counting-the-number-of-messages-a-user-has-and-displaying-the-number/#findComment-781237 Share on other sites More sharing options...
andrew_biggart Posted March 10, 2009 Author Share Posted March 10, 2009 I really need to start learning if statements but yea that has worked thanks alot everyone for your help Quote Link to comment https://forums.phpfreaks.com/topic/148779-solved-counting-the-number-of-messages-a-user-has-and-displaying-the-number/#findComment-781244 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.