Php Mysqli连接为用户''@';本地主机';到数据库';mydb';
我正在尝试编写一个mysql类Php Mysqli连接为用户''@';本地主机';到数据库';mydb';,php,mysqli,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)
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");
?>
我试图删除select_db()部分,并将数据库名称添加到_构造中,但出现了一个不同的错误:
No database selected
我尝试了与此帐户的程序连接,效果良好
顺便说一下,正如您所看到的,第一个错误告诉我,我正在尝试使用一个空白用户名连接
我遗漏了什么吗?删除$this->baglanti->connect()行;假设您的数据库凭据是正确的,那么一切都应该正常工作 中甚至没有提到方法mysqli::connect(),并且已经混淆了其他方法()
No database selected