OO: error
Posted: Tue Aug 09, 2005 6:23 am
Code: Select all
<?php
//DB.class.php
class DbMysql{
var $hostName;
var $userName;
var $password;
var $dbName;
var $dbh;//database connection handle
//constructor and connecting to the database not shown
function executeQuery($query){
if (!is_resource($this->dbh)){
$this->connectDB();//connect to the db if not connected
}
$stmt = new DbMysqlStatement($this->dbh, $query);
return $stmt;//return an object of DbMysqlStatement
}
}
class DbMysqlStatement{
var $query;
var $result;
var $dbh;
function DbMysqlStatement($dbh, $query, $result = ""){
echo "dbmysqlstatement constructor called!!!";
$this->dbh = $dbh;
$this->query = $query;
if (!is_resource($this->result)){
$this->computeResult();//compute result if not computed before
}
}
function computeResult(){
echo "computeresult called!!!";
if (is_resource($this->dbh)){
$this->result = mysql_query($this->query, $this->dbh);
}
}
function fetchRow(){
echo "dbmysqlstatement fetchrow called!!!";
if (is_resource($this->result)){
return mysql_fetch_row($this->result);//return a row
}
}
function fetchAssoc(){
if (is_resource($this->result)){
return mysql_fetch_row($this->result);//return an associative array
}
}
}
?>
<?php
//dbAccess.php
include("DB.class.php");
$dbInstance = new DbMysql();
$dbInstance->connectDb();
$query = "select * from Members_tbl";
$tempArray = array();
$tempArray = $dbInstance->executeQuery($query)->fetchRow();//this doesnot run
//but this runs perfectly and displays all the details in the Members_tbl
//$stmt = $dbInstance->executeQuery($query);
//$tempArray = $stmt->fetchRow();
$iterations = count($tempArray);
//echo $iterations;
for ($i = 0; $i < $iterations; $i++){
echo "<br />".$tempArray[$i];
}
?>Code: Select all
$tempArray = $dbInstance->executeQuery($query)->fetchRow();Code: Select all
which doesnt run but the second set of two lines behind that runs. Why is that?