【问题标题】:Not displaying search results in PHP mysQL在 PHP mySQL 中不显示搜索结果
【发布时间】:2016-03-29 08:28:50
【问题描述】:

问题出在标题中,这是我的search3.php 页面。无论我多么努力地调试它,我仍然不明白为什么它不显示结果,我的数据库被称为“水”,我想从其中的 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>

【问题讨论】:

  • mysqli_real_escape_string 确实是一个冗余函数。请参阅准备好的语句。并且不要混合使用 API。

标签: php mysql search


【解决方案1】:

您正在使用 mysql_num_rows()(来自已弃用的 mysql 模块)而不是 mysqli 中的 mysqli_num_rows()。

改变这一行:

if(mysql_num_rows($raw_results) > 0){

到:

if(mysqli_num_rows($raw_results) > 0){

【讨论】:

    【解决方案2】:

    if(mysql_num_rows($raw_results) &gt; 0)

    应该是

    if(mysqli_num_rows($raw_results) &gt; 0)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-19
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      • 2011-09-22
      • 1970-01-01
      • 2014-01-20
      相关资源
      最近更新 更多