【问题标题】:PHP Warning: mysqli_select_db() expects parameter 1 to be mysqli [duplicate]PHP警告:mysqli_select_db()期望参数1是mysqli [重复]
【发布时间】:2019-03-16 16:38:32
【问题描述】:

我收到这个错误

H01215:PHP 警告:mysqli_select_db() 期望参数 1 为 mysqli 字符串在

当我尝试从 MYSQL 更改为 MYSQLI 时,从我的网站上,为什么我错过了这个。我将每个mysql_ 更改为mysqli_

// Do we have a valid database connection and have we selected a database?
public function databaseSelected()
{
    if(!$this->isConnected()) return false;
    $result = mysql_list_tables($this->name, $this->db);
    return is_resource($result);
}

public function connect()
{
    $this->db = @mysql_connect($this->host, $this->username, $this->password) or $this->notify('Failed connecting to the database with the supplied connection details. Please check the details are correct and your MySQL user has permissions to access this database.<br/><br/>(host: '.$this->host.', user: '.$this->username.', pass: ********)');
    if($this->db === false) return false;
    mysql_select_db($this->name, $this->db) or $this->notify();             if($this->isConnected())
    {
        mysql_set_charset('UTF-8', $this->db);
        $this->query("SET NAMES utf8"); 
    }

    return $this->isConnected();
}

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    mysqli_select_db 期望第一个参数是 mysqli 对象。

    您需要更改参数的顺序,以便第一个参数是 mysqli 对象。

    mysqli_select_db($this->db, $this->name);
    

    【讨论】:

    • 参考方法文档会使答案变得更好......
    • @arkascha 已更新。
    • 所以假设我有这个 $this->result = mysql_query($sql, $this->db) 或 $this->notify(); 如果我将其更改为 $this->result = mysql_query($this->db, $sql) 或 $this->notify(); 会起作用吗?
    • 在我的脑海中,是的(但使用 mysqli_,而不是 mysql_),但你为什么不自己试试看呢?
    【解决方案2】:

    我对您的代码的第一个观察是,当您的代码是 mysql 时,您遇到了 mysqli 错误

    另一个观察结果是您错过了数据库连接的参数顺序。 mysql_select_db 的参数顺序与 mysqli_select_db 不同 示例如下:

    对于 mysql_select_db

    mysql_select_db('db_name', $con);
    

    对于 mysqli_select_db

    mysqli_select_db($con,"db_name");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      • 2012-12-08
      相关资源
      最近更新 更多