我发现了这个错误:
SQLSTATE [](null)(严重性0)致命错误:调用成员函数 查询()在第50行的/.../functions.php中的非对象上
第50行:
foreach($this->sqlDbKowsar->dbh->query($sql) as $row)
DB_Functions
上课:
<?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 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();
}
}
}
我错了什么?
答案 0 :(得分:0)
首先尝试编辑DB_connect类,如下所示:
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;
}
}
然后在DB_Functions编辑
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$this->sqlDbKowsar = new DB_Connect();
$this->sqlDbKowsar->connect();
}
像这样
private $con;
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$this->sqlDbKowsar = new DB_Connect();
$this->con = $this->sqlDbKowsar->connect();
}
然后在foreach循环编辑中
foreach($this->con->query($sql) as $row)
我没有测试实际代码,所以告诉我问题是否仍然存在