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).
 
sorry i can't offer any help but have a couple of questions i have never been able to find the answer to.

why does people use $this a lot? does it do anything extra then just saying $that?

and also i always see it followed by -> which again i have no clue why it is used so would appreciate if you could explain what is it
 
$this has nothing to do with statements - it shows that you're referring to a method or property in the current class context. Allow me to demonstrate:

Code:
class foo {
  public $bar;
  public function __construct($var){
    $this->bar = $var
  }
  public function PrintBar(){
    echo $this->bar;
  }
}
$foo = new foo('foobar');
$foo->PrintBar(); // foobar
echo $foo->bar; //foobar

have a read up on object oriented programming and OOPHP.

with regards to the problem in the OP, I think your problem is that your query returns an array so echoing it won't work as expected and accessing it as you are won't work properly - try var_dump() on your $count variable when you've run your query to see what it's returned. For ease, you might want to alias COUNT(*), (so: SELECT COUNT(*) AS cnt) so you should be able to access it with $count[0]['cnt'].
 
sorry i can't offer any help but have a couple of questions i have never been able to find the answer to.

why does people use $this a lot? does it do anything extra then just saying $that?

and also i always see it followed by -> which again i have no clue why it is used so would appreciate if you could explain what is it

You'll need to read up on object oriented programming (assuming you don't already know what it is) if you want to understand $this :p

If you already understand the basics of OOP, then $this just refers to the parent object of the currently executing code.
 
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>";
}
 
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