无法在数据库中搜索

时间:2015-12-24 04:33:11

标签: php mysql

任何人都可以帮助我吗? 我试图为我的网站创建一个非常基本的搜索功能,但我有一个错误,我无法从我的数据库搜索,错误就像我附加的图像。enter image description here 当我输入数据库中不存在的错误名称时,这是可以的,但是当我输入存在的名称时,它将显示错误,如图像中,任何人都可以帮助我吗? 这是我的PHP代码:

<?php  
require_once("../../include/admin/ad_ovhead.php");
require_once("../../lib/connection.php"); 

$output = '';
if(isset($_POST['search'])){
    $searchq = $_POST['search'];
    $searchq = preg_replace("#[^0-9a-z]#i", "", $searchq);
    $sql = "SELECT * FROM users WHERE firstname LIKE '%$searchq%' OR lastname LIKE '%$searchq%'";

    $query= mysqli_query($conn, $sql) or die ("Can not search");
    $count = mysqli_num_rows($query);
    if($count == 0){
        $output= 'There are no search results!';
    }else{
        while ($row = mysql_fetch_array($query)) {
            $firstname = $row['firstname'];
            $lastname = $row['lastname'];
            $id = $row['id'];

            $output .= '<div> '.$firstname.' '.$lastname.' </div>';
        }
    }

}

?>

1 个答案:

答案 0 :(得分:2)

使用mysqli_num_rows()代替mysql_num_rows()

$count = mysqli_num_rows($query);