I'm learning Object Orïented PHP (or whatever you like to call it) and I ran into a problem.

I made this query class:

class query
{
    var $query;
    var $result;
    var $free;

    function query($query)
    {
        $this->query    = $query;
        $this->free     = false;
        $this->result   = @mysql_query($this->query) or die('Error in mysql query:<br />\n' . mysql_error());
    }
    
    function __destruct()
    {
        if($this->free === false){$this->free(); echo "You forgot to free the query result"; $this->free = true;}
    }

    function array_result()
    {
        for($a = 0; ($b = mysql_fetch_array($this->result, MYSQL_ASSOC)) !== false; $a++)
        {
            $array[$a] = $b;
        }
        
        return $array;
    }

    function get_result()
    {
        return $this->result;
    }
    
    function free()
    {
        @mysql_free_result($this->result) or die("Error while free-ing result.<br />\nObject info:<br />\n" . print_r($this, true));
    }
}

When I execute it I got an error:

Error while free-ing result.
Object info:
query Object (
    [query] => SELECT `style_name`, `style_path` FROM `style` WHERE `style_id`='1'
    [result] => Resource id #4
    [free] =>
)

I don't understand what the error in the code is. I do know this class is mostly useless. It was more to train then to use. The error message if you don't free your query in the destructer is because I want to learn myself to free everything everytime, because some language's require that.

The code owrked when I took it appart, but when I added the mysql_num_row function it crashed:

<?php
mysql_connect('localhost','anonymusius','fake password, so not my real one');
mysql_select_db('anonymusius2');

class query
{
    var $query;
    var $result;
    var $free;

    function query($query)
    {
        $this->query    = $query;
        $this->free     = false;
        $this->result   = @mysql_query($this->query) or die('Error in mysql query:<br />\n' . mysql_error());
    }
    
    function __destruct()
    {
        if($this->free === false){$this->free(); echo "You forgot to free the query result"; $this->free = true;}
    }

    function array_result()
    {
        for($a = 0; ($b = mysql_fetch_array($this->result, MYSQL_ASSOC)) !== false; $a++)
        {
            $array[$a] = $b;
        }
        
        return $array;
    }

    function get_result()
    {
        return $this->result;
    }
    
    function num_rows()
    {
        return @mysql_num_rows($this->result) or die("Error in mysql_num_rows: " . mysql_error());
    }
    
    function free()
    {
        @mysql_free_result($this->result) or die("Error while free-ing result.<br />\nMysql error: " . mysql_error() . "<br />\nObject info:<br />\n" . print_r($this, true));
    }
}

$query = new query("SELECT `style_name`, `style_path` FROM `style` WHERE `style_id`='1'");
echo "Mysql_num_rows(" . $query->num_rows() . ");<br />\n";
print_r($query->array_result());
$query->free();
?>

and I got the error:

Mysql_num_rows(1);
Array (
     [0] => Array
         (
             [style_name] => Basic
             [style_path] => basic 
        )
  ) Error while free-ing result.
Mysql error: 
Object info:
query Object (
     [query] => SELECT `style_name`, `style_path` FROM `style` WHERE `style_id`='1'
     [result] => Resource id #3
     [free] =>
  )

Very sorry to bother you, I forgot to add $this->free = true; to the free function, and because of that the class tried to free the result again in the destructor.

Very sorry for the triple post. Feel free to delete this if you have the power to do so.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.