Mysql错误参数

时间:2014-06-07 11:41:32

标签: php mysql mysqli

我有两个参数错误:

mysqli_select_db()期望参数1为mysqli,给定

为null

mysqli_error()只需要1个参数,0给定

class MyMySQLi
    {
private $connected = false;
private $hostname = "localhost";
private $username = "root";
private $password = "****";
private $database = "****";
private $link;

    public function Connect()
{
     mysqli_select_db($this->link,$this->database) or $this->error(mysqli_error());

    $this->connected = true;
    $this->link = mysqli_connect($this->hostname, $this->username, $this->password) or $this->error(mysqli_error());

}
public function MySQLi($host, $user, $pass, $db)
{
    $this->connected = false;   
    $this->hostname = $host;
    $this->username = $user;
    $this->password = $pass;
    $this->database = $db;
}

public function IsConnected()
{
    if ($this->connected)
    {
        return true;
    }

    return false;
}

谢谢!我还剩一个错误:

mysqli_error()只需要1个参数,0给定

            $this->connected = true;
            $this->link = mysqli_connect($this->hostname, $this->username, 
            $this->password) or $this->error(mysqli_error());
            mysqli_select_db($this->link,$this->database) or $this->error(mysqli_error()); 

2 个答案:

答案 0 :(得分:1)

在设置mysqli_set_db之前,您正在呼叫$this->link。另外mysqli_error需要传递给它的数据库对象:

mysql_error($this->link)

答案 1 :(得分:0)

public function Connect()
{
   $this->link = mysqli_connect($this->hostname, $this->username, $this->password) or $this->error(mysqli_error());
   mysqli_select_db($this->link,$this->database) or $this->error(mysqli_error());
   $this->connected = true;

}

mysqli_connect必须放在mysqli_select_db之前(需要$ this->链接,mysqli_connect的结果)