•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 391,658 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,807 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 978 | Replies: 2
![]() |
•
•
Join Date: Aug 2006
Location: Netherlands
Posts: 223
Reputation:
Rep Power: 2
Solved Threads: 10
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:
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.
•
•
Join Date: Aug 2006
Location: Netherlands
Posts: 223
Reputation:
Rep Power: 2
Solved Threads: 10
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:
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 11:12 am. Reason: Again in trouble :-/
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
Similar Threads
- MySQL++ query question (C++)
- SQL query problem with WHERE clause (ASP)
- Abstract class homework problem (C++)
- gtk.ListStore / gtk.ComboBoxEntry class problem (Python)
- Problem changing contents of JTable when button is pressed... (Java)
- 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)


Linear Mode