当include_once在文件中时,PHP本地主机页面无法加载

时间:2020-11-10 20:36:26

标签: php html mysql sql

因此,我正在关注YouTube上的PHP / SQL教程,这是我在“ index.php”文件中包含的代码:

<?php
    include_once 'includes\dbh.php';
  ?>

<!DOCTYPE html>\
<html>
<head>
    <title>Coupons</title>
</head>
<body>

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

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

</body>
</html>

这是我的“ dbh.php”文件中的代码:

<?php

$dbServername = "localhost:8080";
$dbUsername = "root";
$dbPassword = "";
$dbName = "coupons";

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

?>

现在,我的问题是索引文件中的第一组PHP代码(包括一次)导致页面永远不会加载。如果我取出那部分,页面将加载。所有目录路径都是正确的,所有代码都与视频中的完全相同,并且所有数据库和表都正确存在,所以我不明白为什么该页面无法加载。

2 个答案:

答案 0 :(得分:0)

试试这个 在dbh.php中

 <?php
    $dbServername = "localhost";//made change
    $dbUsername = "root";
    $dbPassword = "";
    $dbName = "coupons";
    $conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
    ?>

在index.php中

    <?php
    include_once 'includes/dbh.php';//made change
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Coupons</title>
    </head>
    <body>
    
    <?php
        $sql = "SELECT * FROM users;";
        $results = mysqli_query($conn,$sql);
        $resultCheck = mysqli_num_rows($results);//made change
    
        if($resultCheck > 0){
            while($row = mysqli_fetch_assoc($results)){
                echo $row['ID'];
            }
        }
    ?>
    
    </body>
    </html>

答案 1 :(得分:-3)

使用

$mysqli = new mysqli($dbServername, $dbUsername, $dbPassword, $dbName);
if ($mysqli -> connect_errno) {
   echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
   exit();
}

查看错误

它所做的是,它不会在连接失败时使应用程序崩溃并向您报告错误。