我正在调用一个类,以通过构造函数方法连接到本地mySql数据库。我启动php文件时收到500错误。我做错了什么吗?我在实例化但无法连接时将主机用户pass和dbName的值传递给类。经测试,数据库运行正常。
<?php
Class MyDB{
public $query;
public $myConnection;
public function__construct($host, $username, $password, $dbname){
$this->myConnection = mysqli_connect($host, $username, $password, $dbname);
}
public function listScores(){
$this->query = "SELECT answer FROM correct_guesses" or die("error ..." . mysqli_error($this->$myConnection));
$result = $this->$myConnection->query($this->query);
while($row = mysqli_fetch_array($result)) {
echo $row["answer"] . "<br>";
}
}
}
$score = new MyDB('localhost','root','root','scoreDB');
$score->listScores();
?>