尝试使用MySqli时,我不断收到这些PHP错误消息

时间:2018-09-08 22:45:20

标签: php mysql

我提供了代码,因此人们可以更好地帮助我。好的,我正在根据YouTuber mmtut的教程编写PHP教程。使用本地PHPMyAdmin,再次检查我的命名约定是否正确,并收到以下两个错误:

  

警告:mysqli_query()期望参数1为mysqli,给定字符串   在第11行的C:\ xampp \ htdocs \ PHPMySQL \ index.php中

     

警告:mysqli_num_rows()期望参数1为mysqli_result,   第12行的C:\ xampp \ htdocs \ PHPMySQL \ index.php中给出的null

我的PHP代码:

在includes文件夹中:     

$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "phplessons";

$conn = "mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName)";

?>

在index.php文件中:

<?php
include_once 'includes/dbh.inc.php';
?>

<!DOCTYPE html>
<html>
<body>

<?php
$sql = "SELECT * FROM posts;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);

if($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row['subject'];
}
}
?>

</body>
</html>

1 个答案:

答案 0 :(得分:3)

您的连接属性是一个文字。您想要这样的东西:

$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);