我创建了一个类来访问我的数据库。简化的类如下(我将其命名为 dbaccess.php)
class dbaccess {
function read($db) {
$con = mysqli_connect($db);
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM equipment");
while($row = mysqli_fetch_array($result)){
$print = $print . $row['ID'] . " " . $row['name'] . " " . $row['new_price'] . " " . $row['residual_value'] . "<br>";
}
echo $print;
mysqli_close($con);
}
}
要访问该课程,我使用此代码
include './dbaccess.php';
//define db address
$add = '"localhost","myuser","mypassword","mydbname"';
$db = new dbaccess;
$db->read($add);
此代码导致
Failed to connect to MySQL: Unknown MySQL server host '"localhost","myuser","mypassword","mydbname"'(2)
我不知道如何解决它,这里有人可以帮助我吗?