php/sql help please :D

Permabanned
Joined
25 Oct 2004
Posts
9,078
Hiya all, been pulling my hair over this as I just cant seem to figure it out, basically what I have an internal message system on an auction site ive helped create for a charity and I've added some additional functionality to it like if a message is read or not, when you replied to it etc

What im attempting to add now is a notification system if you have new messages ie a (3 Unread Messages) i've got the sql code working with phpmyadmin, it returns the correct value np but I just cant seem to get it working within the site.

This is the code i have, the sql_query is echo'd out to verify its ok and its fine, but no result is shown, if i print_r the $count data i get an array but no idea how to display the result.
PHP:
$this->sql_query = "SELECT COUNT(*) FROM ".$this->user_communications_table." WHERE message_to=".$this->classified_user_id." AND message_read!=1";
$count = $db->Execute($this->sql_query);
            
echo $this->sql_query."<br />";
echo "\"".$count."\"";
            
if ($count == 0) {
    $comm = urldecode($this->messages[425])."</span></a></div>";
} else {
    $comm = urldecode($this->messages[425])." (".$show->$count." Unread)</span></a></div>";
}

the two echo's are just for debugging. Any idea why it wont display the count value, in the above case when logged in as site admin it should return 1, and it does if i put the above sql query into phpmyadmin (minus the site specific code that is).
 
Does this work?

PHP:
$this->sql_query = "SELECT COUNT(*) AS Total FROM ".$this->user_communications_table." WHERE message_to=".$this->classified_user_id." AND message_read!=1";
$count = $db->Execute($this->sql_query);
$count = $count['Total'];
            
echo $this->sql_query."<br />";
echo "\"".$count."\"";
            
if ($count == 0) {
    $comm = urldecode($this->messages[425])."</span></a></div>";
} else {
    $comm = urldecode($this->messages[425])." (".$show->$count." Unread)</span></a></div>";
}

the above results in error messages when attempting to run the code, ive managed to resolve it now by changing my code a bit, now have it as follows and it works fine.

PHP:
            //        *******************************************************
            $this->sql_query = "SELECT COUNT(*) FROM ".$this->user_communications_table." WHERE message_to=".$this->classified_user_id." AND message_read!=1";
            $result = mysql_query($this->sql_query);
            
            $count = mysql_result($result, 0);
            
            if ($count == 0) {
                $comm = urldecode($this->messages[425])."</span></a></div>";
            } else {
                $comm = urldecode($this->messages[425])."</span></a> <span style=\"color: #000;font-size: 16px;font-weight:bold;text-decoration: none;\">(".$count." Unread)</span></div>";
            }
            //        *******************************************************

For some reason $db->Execute was causing it to return an array of data which just didnt wanna work right, now it returns the value im after and works as it should. Still quiet new to php/sql so probably something i did wrong.
 
Back
Top Bottom