无法从数据库打印项目

时间:2015-01-19 22:02:25

标签: php html database mysqli mysql-num-rows

我一直在error on line 20, "if(mysqli_num_rows != 0)"。错误如下:

  

"Notice: Use of undefined constant mysqli_num_rows - assumed 'mysqli_num_rows' in C:\wamp\www\movieDB\movies.php on line 20"

这可能只是我忽视的一个愚蠢的错误,但我已经尝试了我能想到的一切。提前谢谢!

enter code here
<?php

$mysqli = NEW MySQLi('localhost', 'root', '', 'movies');    

$resultSet = ("SELECT * FROM movie");



     function query () 
    {
        if(mysqli_num_rows != 0) 
        {
            while ($rows = $resultSet->fetch_assoc()) 
            {
                $title = $rows['title'];
                $date = $rows['releaseDate'];
                $dFirst = $rows['directorFirst'];
                $dLast = $rows['directorLast'];

                    echo "<p>Name: $title, $date<br>Director: $dFirst $dLast";
                }
            }else 
            {
            echo "No results.";
            }
        }

    ?>

HTML

<!DOCTYPE html>
<html>
    <head>
        <title>Movies</title>
        <link rel="stylesheet" type="text/css" href="movies.css">
        <link href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:300' rel='stylesheet' type='text/css'>
            <?php require("movies.php");?>
    </head>

    <body>

        <h1>Hello World!!!</h1>
        <a href="index.php">Home</a>

        <div class="movies">
            <div>
                <?php
                query();
                ?>
            </div>
        </div>
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

mysqli_num_rows需要一个参数来保存结果才能工作。

$resultSet没有打电话。你需要mysqli_query。

试试这个:

function query () 
{
    $mysqli = NEW MySQLi('localhost', 'root', '', 'movies');    
    $resultSet = mysqli_query($mysqli, "SELECT * FROM movie");

    if(mysqli_num_rows($resultSet) != 0) 
    {
        while ($rows = $resultSet->fetch_assoc($resultSet)) 
        {
            $title = $rows['title'];
            $date = $rows['releaseDate'];
            $dFirst = $rows['directorFirst'];
            $dLast = $rows['directorLast'];

                echo "<p>Name: $title, $date<br>Director: $dFirst $dLast";
            }
        }else 
        {
        echo "No results.";
        }
}

答案 1 :(得分:0)

<?php
     function query () 
    {
        $mysqli = NEW MySQLi('localhost', 'root', '', 'movies');    
        $resultSet = ("SELECT * FROM movie");

        if(mysqli_num_rows($resultSet) != 0) 
        {
            while ($rows = $resultSet->fetch_assoc()) 
            {
                $title = $rows['title'];
                $date = $rows['releaseDate'];
                $dFirst = $rows['directorFirst'];
                $dLast = $rows['directorLast'];

                    echo "<p>Name: $title, $date<br>Director: $dFirst $dLast";
                }
            }else 
            {
            echo "No results.";
            }
        }
    ?>

mysqli_num_rows()是函数,它必须包含至少一个参数 http://php.net/manual/en/mysqli-result.num-rows.php