【问题标题】:Warning: mysqli_query() expects parameter 1 to be mysqli, object given in [duplicate]警告:mysqli_query() 期望参数 1 是 mysqli,对象在 [重复]
【发布时间】:2012-11-01 03:47:11
【问题描述】:

我在我的数据库类中以这种方式实例化 $db 对象

class MysqlDB {

    protected $_mysql;
    protected $_where = array();
    protected $_query;
    protected $_paramTypeList;
    protected $_crudType = null;

    public function __construct($host, $username, $password, $db) {
        $this->_mysql = new mysqli($host, $username, $password, $db) or die('There was a problem connecting to the database');
    }


    public function query($query)
    {
        $this->_query = filter_var($query, FILTER_SANITIZE_STRING);

        $stmt = $this->_prepareQuery();
        $stmt->execute();
        $results = $this->_dynamicBindResults($stmt);
        return $results;
    }
        protected function _prepareQuery()
    {
        echo $this->_query;
        if (!$stmt = $this->_mysql->prepare($this->_query)) {
            trigger_error("Problem preparing query", E_USER_ERROR);
        }
        return $stmt;
    }

}

我在另一个类(配置文件类)中使用这个 $db 对象作为链接标识符

$db = new MysqlDb('localhost','root','','xxx');

$query=mysqli_query($db,"SELECT id, parent_id, name FROM categories_general");

我怎样才能摆脱这个错误?实际上,我最近从 MySQL 转到了 mysqli。

【问题讨论】:

  • 试试$db->query( "SELECT id, ...");
  • 如果你使用mysqli对象处理,我建议你使用
  • @air4x 现在显示 undefinec $db。
  • 你正在使用对象或进程吗?
  • MysqlDb 为数据库操作创建的一个类。如果是这样,请向我们展示您的 MysqlDb 代码?

标签: php mysqli


【解决方案1】:

试试

$db = mysqli_connect('localhost','root','XXX','XXX');

【讨论】:

    【解决方案2】:
    $db = new mysqli('localhost','root','','test');
    $sql = "select * from user";
    $result = $db->query($sql);
    var_dump($result->fetch_assoc());
    

    【讨论】:

      【解决方案3】:

      您正在使用 mysqli 的程序样式。所以你必须在 db 类中创建查询方法。

      class db{
        private $connection;
      
        public function __construct(){
          $this->connect();
        }
        public function connect(){
          $this->connection=mysqli_connect('localhost','root','','new5');
        }
        //add these methods
        public function query($query_string){
          return mysqli_query($this->connection, $query_string);
        }
        public function fetch_row($mysqli_result){
          return mysqli_fetch_row($this->connection, $mysqli_result);
        }
        //and other functions you need
      }
      

      或者您可以使用带有扩展方法的面向对象样式;

      class db extends mysqli{
        function __construct($host, $username, $password, $databaseName, $port=3306){
          mysqli::__construct($host, $username, $password, $databaseName, $port);
        }
      }
      

      【讨论】:

        【解决方案4】:

        添加功能

        public function getDB()
        {
            return $this->_mysql;
        }
        
         $query=mysqli_query($db->getDB(),"SELECT id, parent_id, name FROM categories_general");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-26
          • 2012-04-26
          • 1970-01-01
          • 1970-01-01
          • 2014-02-08
          • 2015-06-14
          相关资源
          最近更新 更多