Query class problem

Reply

Join Date: Aug 2006
Posts: 223
Reputation: Anonymusius is on a distinguished road 
Solved Threads: 10
Anonymusius's Avatar
Anonymusius Anonymusius is offline Offline
Posting Whiz in Training

Query class problem

 
0
  #1
Feb 19th, 2007
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:
[php]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));
}
}[/php] When I execute it I got an error:

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

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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 223
Reputation: Anonymusius is on a distinguished road 
Solved Threads: 10
Anonymusius's Avatar
Anonymusius Anonymusius is offline Offline
Posting Whiz in Training

Re: Query class problem

 
0
  #2
Feb 19th, 2007
The code owrked when I took it appart, but when I added the mysql_num_row function it crashed:

[php]<?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();
?>[/php]
and I got the error:

  1. Mysql_num_rows(1);
  2. Array (
  3. [0] => Array
  4. (
  5. [style_name] => Basic
  6. [style_path] => basic
  7. )
  8. ) Error while free-ing result.
  9. Mysql error:
  10. Object info:
  11. query Object (
  12. [query] => SELECT `style_name`, `style_path` FROM `style` WHERE `style_id`='1'
  13. [result] => Resource id #3
  14. [free] =>
  15. )
Last edited by Anonymusius; Feb 19th, 2007 at 12:12 pm. Reason: Again in trouble :-/
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 223
Reputation: Anonymusius is on a distinguished road 
Solved Threads: 10
Anonymusius's Avatar
Anonymusius Anonymusius is offline Offline
Posting Whiz in Training

Re: Query class problem

 
0
  #3
Feb 20th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC