Strange class OOP behavior
Strange class OOP behavior
am 27.08.2007 16:23:07 von Marcel Molenaar
Anyone ever experienced this problem.
When i pass a mysql result to the constructor of a class and use that
resultset inside that class the original resultset outside the class gets
affected too. That is not right i think because my result inside my class is
private. Is this a bug? Can someone look at my code below please:
require_once('includes/connect.php');
class test {
private $_result;
private $_record;
public function __construct($result) {
$this->_result = $result;
}
public function show() {
while($this->_record = mysql_fetch_array($this->_result)) {
echo $this->_record['name'].'
';
}
}
}
# usage
# sql statement dat artikels ophaald uit cartal database
$SQL = "SELECT * FROM test_table";
# uitvoeren van dit statement
$RESULT = mysql_query($SQL,$conn);
$t = new test($RESULT);
$t->show();
// mysql_data_seek($RESULT,0);
// this prints out nothing unless i call mysql_data_seek($RESULT,0) before
this whileloop so that means that the pointer of the recordset had moved to
the end because of the while loop in the class itself...that is strange!
while($RECORD = mysql_fetch_array($RESULT)) {
echo $RECORD['name'].'
';
}
?>
Re: Strange class OOP behavior
am 29.08.2007 07:57:52 von Nithin
Hi i tried with your coding in my local server its working fine for
me..
I thing you may gone wrong some other part of the coding...
test using the following code:
create a database: testdb
A table users add a field 'username'
then try with this coding bellow
$con = mysql_connect('localhost','root','');
$db = mysql_select_db('testdb');
class test {
private $_result;
private $_record;
public function __construct($result) {
$this->_result = $result;
}
public function show() {
while($this->_record = mysql_fetch_array($this->_result)) {
echo $this->_record['username'].'
';
}
}
}
# usage
# sql statement dat artikels ophaald uit cartal database
$sql = "SELECT * FROM users";
# uitvoeren van dit statement
$RESULT = mysql_query($sql);
$t = new test($RESULT);
$t->show();
?>
Re: Strange class OOP behavior
am 29.08.2007 07:57:52 von Nithin
Hi i tried with your coding in my local server its working fine for
me..
I thing you may gone wrong some other part of the coding...
test using the following code:
create a database: testdb
A table users add a field 'username'
then try with this coding bellow
$con = mysql_connect('localhost','root','');
$db = mysql_select_db('testdb');
class test {
private $_result;
private $_record;
public function __construct($result) {
$this->_result = $result;
}
public function show() {
while($this->_record = mysql_fetch_array($this->_result)) {
echo $this->_record['username'].'
';
}
}
}
# usage
# sql statement dat artikels ophaald uit cartal database
$sql = "SELECT * FROM users";
# uitvoeren van dit statement
$RESULT = mysql_query($sql);
$t = new test($RESULT);
$t->show();
?>
Re: Strange class OOP behavior
am 29.08.2007 07:57:52 von Nithin
Hi i tried with your coding in my local server its working fine for
me..
I thing you may gone wrong some other part of the coding...
test using the following code:
create a database: testdb
A table users add a field 'username'
then try with this coding bellow
$con = mysql_connect('localhost','root','');
$db = mysql_select_db('testdb');
class test {
private $_result;
private $_record;
public function __construct($result) {
$this->_result = $result;
}
public function show() {
while($this->_record = mysql_fetch_array($this->_result)) {
echo $this->_record['username'].'
';
}
}
}
# usage
# sql statement dat artikels ophaald uit cartal database
$sql = "SELECT * FROM users";
# uitvoeren van dit statement
$RESULT = mysql_query($sql);
$t = new test($RESULT);
$t->show();
?>