2016-03-29 29 views
1

問題はタイトルにあり、これは私のsearch3.phpページです。私はそれをデバッグしようとしても、結果が表示されない理由は分かりません。データベースは「水」と呼ばれ、3つのテーブルから値を取得したいと思います。投稿、about_general_managers、ヒント。PHPで検索結果を表示しないmysql

mysqli_num_rows()は、パラメータ1がmysqli_fetch_array()でもmysqli_result()

あることを期待し、ディックスことだけについての私に講義しないでくださいと、私は、検索を入力するたびに、エラーが表示されます私がすでにやったのでチュートリアルやものをもっと手に入れても問題は解決できません:

<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> 
+1

mysqli_reaであるべきl_escape_stringは実際には冗長な関数です。準備文を参照してください。また、APIを混在させないでください。 – Strawberry

答えて

2

あなたはmysqliのから)(I _num_rows代わりのMySQL(非推奨mysqlのモジュールから)はmysql_num_rows()を使用しています。

変更この行:

if(mysql_num_rows($raw_results) > 0){ 

へ:

if(mysqli_num_rows($raw_results) > 0){ 
0

if(mysql_num_rows($raw_results) > 0)

if(mysqli_num_rows($raw_results) > 0)

関連する問題