我正在尝试编写一个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
我尝试了与此帐户的程序连接,它工作正常。
顺便说一句,您可以看到第一个错误告诉我正在尝试使用空白用户名连接...
我错过了什么吗?