MySQL Result Object as Class Member Variable??
Posted: Thu Nov 13, 2008 10:56 am
Hey, All. My first post:
require_once("class_db.php"); //class returning result object
class my_class {
var $connection;
var $sql;
var $sql_result;
function __construct() {
$this->connection = new db(); //in require_once statement above
$this->sql = "select * from my_table";
$this->execute_sql;
$this->use_result_A();
$this->use_result_B();
}
private function execute_sql() {
$this->sql_result = $this->connection->query_select($this->sql); //function in db class
}
private function use_result_A() {
while ($row = mysql_fetch_assoc($this->sql_result) {
echo "Hi";
}
}
private function use_result_B() {
while ($row = mysql_fetch_assoc($this->sql_result) {
echo "Hi";
}
}
}
Now, the question. I've written something like the above. use_result_A() works as expected. use_result_B() will never say "Hi". In order to get use_result_B() to say "Hi" the correct number of times (indeed, at all), I must re-perform $this->execute_sql(). Why is that? Shouldn't the MySQL result object persist? In fact, if I just put print_r($this->sql_result) at the beginning of use_result_B(), it prints something like "Resource ID #8". And if I put get_resource_type($this->sql_result), it prints "mysql_result". So, the object does still exist and it is a mysql result object, but it seems as though the object has been de-instantiated or something. All the rows are gone. Does this have to do with garbage collection? If so, how to I tell PHP not to perform collection on this variable?
Thanks in advance for any comments.
require_once("class_db.php"); //class returning result object
class my_class {
var $connection;
var $sql;
var $sql_result;
function __construct() {
$this->connection = new db(); //in require_once statement above
$this->sql = "select * from my_table";
$this->execute_sql;
$this->use_result_A();
$this->use_result_B();
}
private function execute_sql() {
$this->sql_result = $this->connection->query_select($this->sql); //function in db class
}
private function use_result_A() {
while ($row = mysql_fetch_assoc($this->sql_result) {
echo "Hi";
}
}
private function use_result_B() {
while ($row = mysql_fetch_assoc($this->sql_result) {
echo "Hi";
}
}
}
Now, the question. I've written something like the above. use_result_A() works as expected. use_result_B() will never say "Hi". In order to get use_result_B() to say "Hi" the correct number of times (indeed, at all), I must re-perform $this->execute_sql(). Why is that? Shouldn't the MySQL result object persist? In fact, if I just put print_r($this->sql_result) at the beginning of use_result_B(), it prints something like "Resource ID #8". And if I put get_resource_type($this->sql_result), it prints "mysql_result". So, the object does still exist and it is a mysql result object, but it seems as though the object has been de-instantiated or something. All the rows are gone. Does this have to do with garbage collection? If so, how to I tell PHP not to perform collection on this variable?
Thanks in advance for any comments.