Jump to content

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/108834-solved-counting-received-messages/
Share on other sites

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

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!

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

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";
  }
}

?>

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";
  }
}

?>

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";
  }
}

?>

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

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";
}

?>

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";
}
?>

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";

}

?>

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.