php - Fatal error: Call to a member function query() on a non-object -
i've got error:
sqlstate[] (null) (severity 0) fatal error: call member function query() on non-object in /.../functions.php on line 50
line 50 :
foreach($this->sqldbkowsar->dbh->query($sql) $row)
db_functions
class :
<?php class db_functions { private $sqldbkowsar; // constructor function __construct() { require_once 'db_connect.php'; // connecting database $this->sqldbkowsar = new db_connect(); $this->sqldbkowsar->connect(); } // destructor function __destruct() { } function getsimplesearch($title,$cat,$start_end){ $start_and_end = explode("-",$start_end); $cat = $this->getcategories($cat); $array_of_cat = explode(",",$cat); $sql = "select * ( select *,row_number() on (order goodmaincode) row [sarmem].[dbo].[book_data1] goodname ' %$title% ' , groupcode in (" . implode(",",$array_of_cat) . ") ) a.row > $start_and_end[0] , a.row <= $start_and_end[1] "; $mjson = array(); foreach($this->sqldbkowsar->dbh->query($sql) $row){ $arr = array( 'goodmaincode'=>persian_sql_to_php($row['goodmaincode']) ); array_push($mjson,$arr); } //added foreach($mjson $v){ if(!isset($result[$main_good_code = $v['goodmaincode']])){ $result[$main_good_code = $v['goodmaincode']] = $v; } else $result[$main_good_code = $v['goodmaincode']]['amount'] += $v['amount']; } return $result ; }//end simple search function } ?>
db_connect
class :
class db_connect { public $dbh; // constructor function __construct() { } // destructor function __destruct() { // $this->close(); } // connecting database public function connect() { require_once 'config.php'; try { $hostname = db_host ; $dbname = db_database; $port = 1433; $this->dbh = new pdo ("dblib:host=$hostname;dbname=$dbname;charset=utf-8",db_user, db_password); } catch(pdoexception $e) { echo $e->getmessage(); } } }
what wrong ?
first try , edit db_connect class this:
class db_connect { public $dbh; // constructor function __construct() { require_once 'config.php'; try { $hostname = db_host ; $dbname = db_database; $port = 1433; $this->dbh = new pdo ("dblib:host=$hostname;dbname=$dbname;charset=utf-8",db_user, db_password); } catch(pdoexception $e) { echo $e->getmessage(); } } // destructor function __destruct() { // $this->close(); } // connecting database public function connect() { return $this->dbh; } }
then in db_functions edit
function __construct() { require_once 'db_connect.php'; // connecting database $this->sqldbkowsar = new db_connect(); $this->sqldbkowsar->connect(); }
like this
private $con; function __construct() { require_once 'db_connect.php'; // connecting database $this->sqldbkowsar = new db_connect(); $this->con = $this->sqldbkowsar->connect(); }
then in foreach loop edit to
foreach($this->con->query($sql) $row)
i didn't tested actual code tell me if problem persists
Comments
Post a Comment