mysqli_num_rows() expects parameter 1 to be mysqli_result php

时间:2016-11-12 06:00:24

标签: php mysql

i created a search function that search and retrieve data in database but got this error

mysqli_num_rows() expects parameter 1 to be mysqli_result php in line 16

here is my php code

<?php
    include 'database_conn.php';

    //collect search title
    if (isset($_GET['keywords'])){

        $searchq = $_GET['keywords'];
        $searchq = preg_replace("#[^a-z0-9]#i" , "", $searchq);

        $query = "SELECT eventTitle FROM te_events WHERE eventTitle LIKE '%searchq%'";

        mysqli_query($conn, $query) 
        or die ("SQL error:" .mysqli_error($conn));


        $count = mysqli_num_rows($query);

        if($count==0){
            echo "<p>There was no search result!</p>\n";
        }

        else{
            while ($row = mysqli_fetch_array($query)){
                $title = $row['eventTitle'];
                $id    = $row['eventID'];

                echo "<p>title</p>\n";
            }
        }
    }
?>

is there any problem in my code???

1 个答案:

答案 0 :(得分:1)

2 Mistakes in your code

  1. Use '%$searchq%' instead of '%searchq%'.

  2. Replace mysqli_query($conn, $query) with $queryresult = mysqli_query($conn, $query) and use $queryresult for count and result array.

Replace your code with this

<?php
    include 'database_conn.php';

    //collect search title
    if (isset($_GET['keywords'])){

        $searchq = $_GET['keywords'];
        $searchq = preg_replace("#[^a-z0-9]#i" , "", $searchq);

        $query = "SELECT eventTitle FROM te_events WHERE eventTitle LIKE '%$searchq%'";

        $queryresult = mysqli_query($conn, $query) 
        or die ("SQL error:" .mysqli_error($conn));


        $count = mysqli_num_rows($queryresult);

        if($count==0){
            echo "<p>There was no search result!</p>\n";
        }

        else{
            while ($row = mysqli_fetch_array($queryresult)){
                $title = $row['eventTitle'];
                $id    = $row['eventID'];

                echo "<p>title</p>\n";
            }
        }
    }
?>