不在PHP-mysQL中显示搜索结果


Not displaying search results in PHP mysQL

问题在标题中,这是我的search3.php页面。无论我多么努力地调试它,我仍然不明白为什么它不显示结果,我的数据库被称为"water",我想从其中的3个表中获取值:posts、about_general_managers和tips。

每次我输入搜索,错误都会显示为

mysqli_num_rows()期望参数1为mysqli_result()

还有mysqli_fetch_array(),请不要做白痴,告诉我要获得更多的教程和东西,因为我已经做了,但仍然无法解决问题:

<div class="container">
    <div class="row">
            <div class="box">
              <div class="col-lg-12">
                    <hr>
                    <h2 class="intro-text text-center">Search <strong>Results</strong></h2>
                    <hr>
                </div>
 <?php                     
$servername = "localhost";
                    $username = "root";
                    $password = "";
                    $dbname = "water";
                    // Create connection
                    $conn = new mysqli($servername, $username, $password, $dbname);
                    // Check connection
                    if ($conn->connect_error) {
                        die("Connection failed: " . $conn->connect_error);
                    }

$query = $_GET['query']; 
// gets value sent over search form
$min_length = 3;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
    $query = htmlspecialchars($query); 
    // changes characters used in html to their equivalents, for example: < to &gt;
    $query = mysqli_real_escape_string($conn, $query);
    // makes sure nobody uses SQL injection
    $raw_results = mysqli_query($conn,"SELECT * FROM posts
        WHERE ('post_keyword' LIKE '%".$query."%')");
    // * means that it selects all fields, you can also write: `id`, `title`, `text`
    // articles is the name of our table
    // '%$query%' is what we're looking for, % means anything, for example if $query is Hello
    // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
    // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'
    if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
        while($results = mysqli_fetch_array($raw_results)){
        // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
        ?>
            <div class="col-lg-12">
                    <p align="center"> 
                        <a href="pages.php?id=<?php echo $post_id; ?>"><?php echo $results['post_title']; ?></a>
                    </p>
            </div>
            <div class="col-lg-12">
                    <p align="center"> 
                        <?php echo $results['post_content']; ?>
                    </p>
            </div></div></div></div>
        <?php
            // posts results gotten from database(title and text) you can also show id ($results['id'])
        }
    }
    else{ // if there is no matching rows do following
        ?>
                  <div class="col-lg-12">
                    <p align="center"> 
                    No results.
                    </p>
                    </div></div></div></div>
                  <?php
    }
}
else{ // if query length is less than minimum
    echo "Minimum length is ".$min_length;
}

                    $conn->close();
 ?>
    </div><!--DIV to STRETCH FOOTER TO 100% in WIDTH-->
    </div><!--DIV to STRETCH FOOTER TO 100% in WIDTH-->
    </div><!--DIV to STRETCH FOOTER TO 100% in WIDTH-->
    <!--INCLUDE FOR FOOTER-->
    <div><?php include("includes/footer.php");?></div>
    <!--INCLUDE FOR SCRIPTS-->
    <div><?php include("includes/scripts home.php");?></div>

</body>
</html>

这是我的搜索栏所属的页面:

   <form style:"background-color:#f3f3f3;"  action="search3.php" method="GET">
                    <input type="text" name="query"   id="tfq" maxlength="120" placeholder="Search Water District Android Meter Reader"/>   <input type="submit" name="search" id="tfq" value="Search" />
                </form>

您使用的是mysql_num_rows()(来自不推荐使用的mysql模块),而不是mysqli中的mysqli_num_rrows()。

更改此行:

if(mysql_num_rows($raw_results) > 0){

至:

if(mysqli_num_rows($raw_results) > 0){

if(mysql_num_rows($raw_results) > 0)

应该是

if(mysqli_num_rows($raw_results) > 0)