Re: PHP5 & classes & mysql bug ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Viacheslav Kaloshin wrote:
Here is testcase

PHP 5.0.3 (cli) (built: Dec 17 2004 10:47:41)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.3, Copyright (c) 1998-2004 Zend Technologies


<?php
class MySQL {
private $mysql_link=0;
private $mysql_result=0;

function __construct() {
$host="localhost";
$basename="test";
$username="test";
$userpassword="";

$link=mysql_pconnect($host,$username,$userpassword);
if($link)
{ mysql_select_db($link); $this->$mysql_link=$link;
echo "At constructor: ";
echo $this->$mysql_link;
echo "\n";
return 0;
}
else
{
return 1;
}
}
function __destruct() {
echo "At destructor ";
echo $this->$mysql_link;
echo "\n";
if($this->$mysql_link) { mysql_close($this->$mysql_link); }
}

function query($query_string) {

echo "before query: ";
echo $this->$mysql_link;
echo "\n";
$this->$mysql_result=mysql_query($query_string);
// can change to this
#$this->$mysql_result=mysql_query($query_string, $this->$mysql_link);
echo "after query: ";
echo $this->$mysql_link;
echo "\n";
if($this->$mysql_result)
{ return 0; }
else
{ return 1; }
}
function result() {
return mysql_num_rows($this->$mysql_result);
}
function fetch() {
return mysql_fetch_array($this->$mysql_result, MYSQL_NUM);
}
function clear() {
return mysql_free_result($this->$mysql_result);
}

}


$m= new MySQL();

$m->query("select 1+2");
$res=$m->result(); $res=$m->fetch();
$m->clear();
$m->query("select 2+3");
$res=$m->result();
$res=$m->fetch();
$m->clear();


?>

this script output should shwo the same id. but in my case i see next lines:

At constructor: Resource id #4
before query: Resource id #4
after query: Resource id #5
before query: Resource id #5
after query: Resource id #6
At destructor Resource id #6

What i am not understand?

You just posted a mail saying problem solved, RTFM - but I just spent a few minutes tracking it down before realizing it was staring me in the face - and I didn't want others to miss out on the fun.


Basically, it boils down to the fact that you were accessing the same objects properties throughout all your accesses within the class. Namely, $this->who_fricking_knows. (technically $this->null I suppose... interesting question, don't feel like creating a dummy class just to find out though).

$this->$mysql_link should have been $this->mysql_link
$this->$mysql_result should have been $this->mysql_result
etc.

dynamic variables are very handy however. You'll find yourself using them a lot if you have lots of similar tables/etc. that you need to write the same 'thing' for over and over again.

Cheers,
--
- Martin Norland, Database / Web Developer, International Outreach x3257
The opinion(s) contained within this email do not necessarily represent those of St. Jude Children's Research Hospital.


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux