MySQL数据没有显示在PHP网页中


MySQL data not getting displayed in a PHP webpage

我试图从MySQL中获取一些数据,以显示在PHP网页上,但没有显示任何数据。

我在HTML代码的更高层声明了变量searchtermsearchtype。HTML代码只有一个下拉菜单选项和一个供用户搜索的选项。

然而,当我运行代码时,不会显示任何数据。我得到的只是:"找到的项目数:"这就是数据的显示位置。

我拥有的完整HTML和PHP代码如下所示。

<html>
    <body>
        <h1>Search</h1>
        <form action="list_projects.php" method="POST">
            <p>Choose Search Type: <br /></p>
            <select name="searchtype">
                <option value="projectNo">Project Number</option>
                <option value="pjname">Project Name</option>
                <option value="city">Project City</option>
            </select>
            <br />
            <p>Enter Search Term: </p>
            <input name="searchterm" type="text" size="20"/>
            <br />
            <input type="submit" name="submit" value="Search"/>
        </form>
        <?php
            $hostname='mysql.uniwebsite.ac.uk';
            $database='somedatabase';
            $username='uniusername';
            $password='unipassword';
            $link = mysqli_connect($hostname, $username, $password);
            if (!$link) {
                die('Connection failed: ' . mysqli_error());
            }
            $searchtype=$_POST['searchtype'];
            $searchterm=trim($_POST['searchterm']);
            if (!$searchtype || !$searchterm) {
                echo 'No search details. Go back and try again.';
                exit;
            }
            $query = "select * FROM tables WHERE ".$searchtype." like '%".$searchterm."%'";
            $result = mysqli_query($link, $query);
            $num_results = mysqli_num_rows($result);
            echo "<p>Number of projects found: ".$num_results."</p>";
            for ($i=0; $i <$num_results; $i++) {
                $row = mysqli_fetch_assoc($result);
                echo "<p><strong>".($i+1).". Project Number: ";
                echo htmlspecialchars(stripslashes($row['projectNo']));
                echo "</strong><br />Project Name: ";
                echo stripslashes($row['pjname']);
                echo "<br />Project City: ";
                echo stripslashes($row['city']);
                echo "</p>";
            }
            $mysqli_free_result($result);
            $mysqli_close($link);
        ?>
    </body>
</html>

当我打开错误报告时,会出现以下错误:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /home/list_projects.php on line 39
Number of projects found:

Notice: Undefined variable: mysqli_free_result in /home/list_projects.php on line 52
Fatal error: Function name must be a string in /home/list_projects.php on line 52

一些改进的代码可以帮助您,请参阅Change:Homework:行:

<html>
    <body>
        <h1>Search</h1>
        <form action="list_projects.php" method="POST">
            <p>Choose Search Type: <br /></p>
            <select name="searchtype">
                <option value="projectNo">Project Number</option>
                <option value="pjname">Project Name</option>
                <option value="city">Project City</option>
            </select>
            <br />
            <p>Enter Search Term: </p>
            <input name="searchterm" type="text" size="20"/>
            <br />
            <input type="submit" name="submit" value="Search"/>
        </form>
        <?php
            // Change 1: Enable error reporting
            error_reporting(E_ALL);
            ini_set('display_errors', 1);
            $hostname='mysql.uniwebsite.ac.uk';
            $database='somedatabase';
            $username='uniusername';
            $password='unipassword';
            // Change 2: Add $database
            $link = mysqli_connect($hostname, $username, $password, $database);
            // Change 3: Literally copy&paste error checking from mysqli_query docs page
            /* check connection */
            if (mysqli_connect_errno()) {
                printf("Connect failed: %s'n", mysqli_connect_error());
                exit();
            }
            // Homework: Check for legal values in more detail
            $searchtype=$_POST['searchtype'];
            $searchterm=trim($_POST['searchterm']);
            if (!$searchtype || !$searchterm) {
                echo 'No search details. Go back and try again.';
                exit;
            }
            // Homework: Use prepared statements to avoid sql injection
            $query = "select * FROM tables WHERE ".$searchtype." like '%".$searchterm."%'";
            // Change 4: Error checking
            if ($result = mysqli_query($link, $query))
            {
                $num_results = mysqli_num_rows($result);
                echo "<p>Number of projects found: ".$num_results."</p>";
                for ($i=0; $i <$num_results; $i++) {
                    $row = mysqli_fetch_assoc($result);
                    echo "<p><strong>".($i+1).". Project Number: ";
                    echo htmlspecialchars(stripslashes($row['projectNo']));
                    echo "</strong><br />Project Name: ";
                    echo stripslashes($row['pjname']);
                    echo "<br />Project City: ";
                    echo stripslashes($row['city']);
                    echo "</p>";
                }
                // Change 5.1: Remove $
                mysqli_free_result($result);
            } else {
                printf("Error: %s'n", mysqli_error($link));
            }
            // Change 5.2: Remove $
            mysqli_close($link);
        ?>
    </body>
</html>