Mysqli连接给用户'''localhost'访问被拒绝访问数据库'mydb'

时间:2012-05-13 10:28:56

标签: php mysqli

我正在尝试编写一个mysql类......

enter code here
class ASQL
{

public $baglanti;


   public function __construct()
{
    $host = "localhost";
    $user = "root";
    $password = "";
    $db = "mydb";



    $this->baglanti = new mysqli($host, $user, $password)
            or die('Could not connect to the database server' . mysqli_connect_error());

    $this->baglanti -> connect();

    if(!$this->baglanti -> select_db($db)){
        echo "Check the DB name!";
        echo "<br>";
        $this->baglanti->error;
        echo "<br>";
    }

}

public function Q( $query )
{
    if ($stmt = $this -> baglanti -> prepare($query))
    {
        $stmt -> execute();
        $result = $stmt -> get_result();
        while ($myrow = $result -> fetch_assoc())
        {
            var_dump($myrow);
        }
    }
    else
    {
        var_dump($this -> baglanti);
    }





}

public function __destruct()
{
    $this -> baglanti -> close();
}



}

$vt = new ASQL;
$vt -> Q("SELECT * FROM mytable");
?>

此代码给我一个错误

Access denied for user ''@'localhost' to database 'mydb'

我试图删除select_db()部分并将数据库名称添加到__construct但是我遇到了一个不同的错误:

No database selected

我尝试使用此帐户进行程序连接,但效果很好。

顺便说一句,你可以看到第一个错误告诉我要连接空白用户名......

我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

删除行$ this-&gt; baglanti-&gt; connect();并假设您的数据库凭据是正确的,一切都应该正常。

documentation中甚至没有提到mysqli :: connect()方法,并且已经混淆了其他人(a-bug-in-mysqli-connect-method