any reason why variable code isn't couting?

<?php
class db
{
      public function query($query)
      {
            if(isset($this->code))
                  $this->code = $this->code+1;
            else
                  $this->code = 0;
                  
            return mysql_query($query);
      }
      public function fetch_array($query)
      {
            return mysql_fetch_array($query);      
      }
}

$db = NEW db();

$q = $db->query("select * table");

for($i = 0; $row = $db->fetch_array($q); ++$i)
{
      echo $row['progname']."<br />";
}

echo "value :";
echo $db->code;

?>

basically, when I access $db->code it says "0" always... any reason why it isn't counting the number of queries?  there is about 1400 there... so I know that it is function as I intended it to, just not counting... any ideas?
LVL 1
lamerhooDJVAsked:
Who is Participating?
 
ljw87505Connect With a Mentor Commented:
lamerhooDJV,

I might not understand something here, but you are only incrementing $this->code in the query method which you only call once (right after constructing $db).  Is there some other code that is calling $db->query() with the same $db instance that should be incrementing $this->code()?

0
 
PromethylCommented:
Don't you have to define the variable as global in the class scope?

0
 
lamerhooDJVAuthor Commented:
Not quite sure, but that wouldn't explain that it passes the 0 to it within the scope.  And then when I access it, it puts just 0.  It's being set and able to be seen, but it won't count.  So there is something going on with the life of the variable.  Like it's leaving the scope and then being recreated.  I think.  Not sure though, that's why I'm here :)
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
lamerhooDJVAuthor Commented:
oh i see what you are saying, alright... works now :) thanks man
0
 
PromethylCommented:
Share with us the working code?
0
 
lamerhooDJVAuthor Commented:
class db
{
      public function query($query)
      {
            if($this->code != 0)
                  ++$this->code;
            else
                  $this->code = 1;
            
            return mysql_query($query);
      }
      public function fetch_array($query)
      {
            if($this->code != 0)
                  ++$this->code;
            else
                  $this->code = 1;
            return mysql_fetch_array($query);      
      }
}

$db = NEW db();

$q = $db->query("select * from universal_programs");

for($i = 0; $row = $db->fetch_array($q); ++$i)
{
      echo $row['progname']."<br />";
}

echo "value :";
echo $db->code;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.