Jump to content

[SOLVED] Counting the number of messages a user has and displaying the number!


andrew_biggart

Recommended Posts

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'];
?>

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'];
?>

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'];
?>)

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.