找出这串代码的错误并给出正确答案<?php $servername = "127.0.0.1"; $username = "root"; $password = "123456" $dbname="stu_result"; $conn = new mysqli($servername,$username,$password,$dbname); @mysqli_set_charset($conn,utf8); if($conn->connect_eorror) { die("连接失败:" . $conn->connect_error); } ?>
时间: 2023-12-22 07:04:57 浏览: 133
PHP連接MySQL數據庫的幾種方法
这段代码的错误是缺少一个分号在第 3 行,导致语法错误。正确的代码如下所示:
```
<?php
$servername = "127.0.0.1";
$username = "root";
$password = "123456"; // 加上分号
$dbname = "stu_result";
$conn = new mysqli($servername, $username, $password, $dbname);
@mysqli_set_charset($conn, "utf8"); // 加上双引号
if ($conn->connect_error) {
die("连接失败:" . $conn->connect_error);
}
?>
```
阅读全文