2015-06-19 68 views
0

我得到這個錯誤的:致命錯誤:調用一個成員函數查詢()非對象

SQLSTATE[] (null) (severity 0) Fatal error: Call to a member function query() on a non-object in /.../functions.php on line 50

線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(); 

    } 

} 

} 

我的錯誤是什麼?

+1

這是什麼'的var_dump($這個 - > sqlDbKowsar->胸徑);'輸出ķ – D4V1D

+0

@ D4V1D:沒有! –

+0

* Nothing *不是PHP中的一種變量。是'null','empty','string'等? – D4V1D

回答

0

第一次嘗試和編輯DB_connect類是這樣的:

在DB_Functions編輯
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; 

} 

} 

然後

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) 

我沒有測試實際的代碼,所以告訴我,如果問題仍然存在

相關問題