试图为我的while循环获取“找不到名称”


Trying to get a "Name not found." for my while loop

我正在尝试使用 while 循环为我的输出制作条件循环。这是我的输出:

警告:mysqli_num_rows(( 期望参数 1 mysqli_result,资源在第 26 行的 C:''xampp''htdocs''junhao''doLandingPage.php 中给出 找不到名称。

数据库函数.php:

$host = 'localhost';
$username = 'root'; //Change to your own one
$password = ''; //Change to your own one
$db = 'demo'; //Change to your own one
// Connect to the server
$connect = mysql_connect('localhost', 'root', '');
// Connect to the database
mysql_select_db('demo');

doLandingPage.php:

include 'dbFunctions.php';
$search = $_POST['search'];
// Query the database
$query = mysql_query("SELECT * FROM guests WHERE name = '$search'");
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>TITLE HERE.</title>
        <link rel="stylesheet" type="text/css" href="stylesheet.css">
        <!-- Foundation -->
        <link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.1.2/foundation.min.css" rel="stylesheet" media="screen" />
        <link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.1.2/foundation.min.js" media="screen">
    </head>    
    <body>
    <?php 
    if(mysqli_num_rows($query)>0){
        while($row = mysqli_fetch_array($query))
        {
            $id = $rows['id'];
            $name = $rows['name'];
            $table = $rows['table'];
            echo "<h1>$name</h1>"
            echo "<h2>$table</h2>"
        }
    }else{
        echo "<tr><td colspan='4'>Name not found.";
    }
    ?>
    </body>
</html>

您必须先运行查询。您正在尝试对常规字符串使用mysqli_num_rows()

你混合了mysql和mysqli,因为两者都是不同的,请注意mysql已被弃用使用mysqli。

试试这个代码:-

<?php 
         if(mysql_num_rows($query)>0){
                while($row = mysql_fetch_array($query))
                {
                    $id = $row['id'];
                    $name = $row['name'];
                    $table = $row['table'];
                    echo "<h1>$name</h1>"
                    echo "<h2>$table</h2>"
                }
          }else{
                echo "<tr><td colspan='4'>Name not found.';
          }
    ?>