PHP LIKE无效

时间:2015-12-26 12:45:56

标签: php mysql sql

我有这个PHP代码

$search=$_GET['Searchusername'];
$searchuser="SELECT fullname,lastname,userName FROM users WHERE userName LIKE '%{$search}%' 
OR fullname LIKE '%{$search}%' OR LIKE '%{$search}%'";

$query = mysqli_query($link, $searchuser);
$numrows = mysqli_num_rows($query);
$row = mysqli_fetch_row($query);

但它给了我2个错误

mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in 

并且

mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolean given in

我想让它与LIKE运算符一起使用。

1 个答案:

答案 0 :(得分:2)

Firstoff,LIKE是SQL语法的一部分,而不是PHP。在您的查询中,您缺少列名称,这就是它失败的原因:

$searchuser="SELECT fullname,lastname,userName 
             FROM users 
             WHERE userName LIKE '%{$search}%' 
             OR fullname LIKE '%{$search}%' 
             OR lastname LIKE '%{$search}%'";

您获得的布尔值为false,因为查询因lastname丢失而失败。