为什么mysqli_fetch_array($ result)返回NULL?以及如何解决这个问题?

时间:2018-12-05 23:12:01

标签: javascript php function session mysqli-fetch-array

这个问题是重复的问题,但我仍在问,因为我认为我的情况有所不同。

我正在尝试创建一个网站,其中在网站上显示电影海报,并且单击海报时,它应该显示一个大的弹出框,而电影的标题应该在该弹出框上显示。我无法在网站上显示电影的标题。

includes/dbh.inc.php:

<?php
    $dbServername = "localhost";
    $dbUsername = "root";
    $dbPassword = "";
    $dbName = "website";
    $conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

index.php:

<?php
    session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
</head>
<body>
<?php
    if (isset($_SESSION['account_id'])) {
        include_once 'after_login.php';
    } else {
        include_once 'login.php';
    }
?>
</body>
</html>

after_login.php:

<style>
    body {
        overflow: scroll;
        background-color: rgb(39, 40, 41);
    }

    img {
        width: 216px;
        height: 312px;
        margin: 5px 10px 5px 10px;
        float: left;
    }

    #navigation {
        background-color: skyblue;
        width: 100%;
        height: 40px;
    }

    #content {
        width: 80%;
        margin: 0px auto 0px auto;
    }

    #pop_up {
        width: 80%;
        height: 90%;
        visibility: hidden;
        background-color: skyblue;
        margin: 0px 10% 0px 10%;
        position: fixed;
    }

    #pop_up_close {
        width: 50px;
        height: 50px;
        float: right;
        background-color: black;
    }

    #pop_up_content {
        margin: 50px 0px 0px 0px;
    }
</style>
<div id="navigation">
    <form action="includes/logout.inc.php" method="POST">
        <button type="submit" name="submit">Logout</button>
    </form>
</div>
<div id="content">
    <?php
        include "includes/dbh.inc.php";
        $sql = "SELECT * FROM film ORDER BY release_date DESC;";
        $result = mysqli_query($conn, $sql);
        while ($row = mysqli_fetch_assoc($result)) {
            echo ("<img src='../film_poster/".$row['id'].".jpg' onClick='pop_up(".$row['id'].")'>");
        }
    ?>
</div>
<div id="pop_up">
    <div id="pop_up_close"></div>
    <div id="pop_up_content"></div>
</div>
<script>
    var container = document.getElementById("pop_up");
    document.getElementById("pop_up_close").onclick = function () {
        document.getElementById("pop_up_content").innerHTML = "";
        container.style.visibility = "hidden";
    }
    function pop_up(id) {
        document.getElementById("pop_up_content").innerHTML = "<?php include 'includes/film_info.inc.php'; get_info('" + id + "');?>";
        container.style.visibility = "visible";
    }
</script>

film_info.inc.php:

<?php
    function get_info($id) {
        include "includes/dbh.inc.php";
        $sql = "SELECT * FROM film WHERE id=".$id.";";
        $result = mysqli_query($conn, $sql);
        $row = mysqli_fetch_array($result);

        if ($row !== NULL && $result) {
            echo ("YES: ".$row['title']);
        } else {
            echo ("NO: ".$sql);
        }
    }
?>

DataBase ScreenShot:

enter image description here

0 个答案:

没有答案