试图找到一个"名称未找到。"为我的while循环

时间:2016-02-17 09:38:58

标签: php html while-loop

我试图通过使用while循环为我的输出创建一个条件循环。这是我的输出:

  

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

dbFunctions.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>

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

你混合了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.';
          }

    ?>