| | |
Query class problem
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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:
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.
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:
PHP Syntax (Toggle Plain Text)
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]<?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:
[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:
PHP Syntax (Toggle Plain Text)
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] => )
Last edited by Anonymusius; Feb 19th, 2007 at 12:12 pm. Reason: Again in trouble :-/
![]() |
Similar Threads
- Problem changing contents of JTable when button is pressed... (Java)
- MySQL++ query question (C++)
- SQL query problem with WHERE clause (ASP)
- Abstract class homework problem (C++)
- gtk.ListStore / gtk.ComboBoxEntry class problem (Python)
- Derived class problem (C)
Other Threads in the PHP Forum
- Previous Thread: ok what have i done wrong :(
- Next Thread: Always return the same date(s)
| Thread Tools | Search this Thread |
301 apache api array autosuggest beginner binary broken cakephp checkbox class cms code compression cron curl data database date display dropdownlist dynamic echo email eregi error execution file files folder form forms function functions google href htaccess html httppost if...loop image include insert ip javascript joomla jquery key library limit link links login mail md5 menu mlm multiple mysql mysql_real_escape_string oop paypal pdf pdfdownload php phpvotingscript problem query radio random recursion remote screen script search searchbox server session sessions sms sorting source space sql syntax system table tutorial update upload url validator variable video volume votedown web website youtube zend





