【问题标题】:Fatal error: Call to a member function query() on a non-object致命错误:在非对象上调用成员函数 query()
【发布时间】:2015-06-19 07:49:27
【问题描述】:

我遇到了这个错误:

SQLSTATE[] (null) (severity 0) 致命错误:调用成员函数 query() 在第 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();

    }

}

}

我怎么了?

【问题讨论】:

  • var_dump($this-&gt;sqlDbKowsar-&gt;dbh); 输出什么?k
  • Nothing 不是 PHP 中的一种变量。是null、empty、string等吗?

标签: php pdo


【解决方案1】:

首先尝试像这样编辑 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)

我没有测试实际代码,如果问题仍然存在,请告诉我

【讨论】:

    猜你喜欢
    • 2015-04-14
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    相关资源
    最近更新 更多