簡體   English   中英

在PHP mysQL中不顯示搜索結果

[英]Not displaying search results in PHP mysQL

問題在標題中,這是我的search3.php頁面。 無論我多么努力地調試它,我仍然不明白為什么它不顯示結果,我的數據庫稱為“水”,我想從其中的3個表中獲取值:posts,about_general_managers和技巧。

每次我輸入搜索內容時,錯誤都會顯示為

mysqli_num_rows()期望參數1為mysqli_result()

也可以使用mysqli_fetch_array() ,請不要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的模塊),而不是MySQL的 _num_rows()從mysqli的。

更改此行:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM