I've got this error:
SQLSTATE[] (null) (severity 0) Fatal error: Call to a member function query() on a non-object in /.../functions.php on line 50
line 50 :
foreach($this->sqlDbKowsar->dbh->query($sql) as $row)
DB_Functions
class :
<?php
class DB_Functions {
private $sqlDbKowsar;
// constructor
function __construct() {
require_once 'DB_Connect.php';
// connecting to 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 * from (
select *,ROW_NUMBER() OVER (ORDER BY GoodMainCode) as row from [SarMem].[dbo].[Book_Data1] WHERE GoodName LIKE ' %$title% ' and
GroupCode IN (" . implode(",",$array_of_cat) . ") ) a
where a.row > $start_and_end[0] and a.row <= $start_and_end[1] ";
$mjson = array();
foreach($this->sqlDbKowsar->dbh->query($sql) as $row){
$arr = array(
'GoodMainCode'=>persian_sql_to_php($row['GoodMainCode'])
);
array_push($mjson,$arr);
}
//added
foreach($mjson as $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 to 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 is my wrong ?
First try and edit the DB_connect class like 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 to database
public function connect() {
return $this->dbh;
}
}
Then in DB_Functions edit
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$this->sqlDbKowsar = new DB_Connect();
$this->sqlDbKowsar->connect();
}
like this
private $con;
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$this->sqlDbKowsar = new DB_Connect();
$this->con = $this->sqlDbKowsar->connect();
}
Then in foreach loop edit to
foreach($this->con->query($sql) as $row)
I didn't tested the actual code so tell me if problem persists
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.