User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Aug 2006
Location: Netherlands
Posts: 223
Reputation: Anonymusius is on a distinguished road 
Rep Power: 2
Solved Threads: 10
Anonymusius's Avatar
Anonymusius Anonymusius is offline Offline
Posting Whiz in Training

Query class problem

  #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:

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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2006
Location: Netherlands
Posts: 223
Reputation: Anonymusius is on a distinguished road 
Rep Power: 2
Solved Threads: 10
Anonymusius's Avatar
Anonymusius Anonymusius is offline Offline
Posting Whiz in Training

Re: Query class problem

  #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:

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 :-/
Reply With Quote  
Join Date: Aug 2006
Location: Netherlands
Posts: 223
Reputation: Anonymusius is on a distinguished road 
Rep Power: 2
Solved Threads: 10
Anonymusius's Avatar
Anonymusius Anonymusius is offline Offline
Posting Whiz in Training

Re: Query class problem

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 1:35 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC