2014-09-23 150 views
0

我有这样的代码:mysqli_num_rows()错误没有返回字符串

$con=mysqli_connect("example","example","example","example"); 
function check_input($value) 
{ 
    global $con; 
    // Stripslashes 
    if (get_magic_quotes_gpc()) 
    { 
     $value = stripslashes($value); 
    } 
    // Quote if not a number 
    if (!is_numeric($value)) 
    { 
     $value = "'" . mysqli_real_escape_string($con, $value) . "'"; 
    } 
    return $value; 
} 
$checklogin = mysqli_query($con, "SELECT username, password FROM users WHERE username LIKE '".check_input($_POST['username'])."';"); 
if(mysqli_num_rows($checklogin) == 0) 
{ 
    echo "Username doesn't exist"; 
} 

而且我得到这个错误:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /home/u436120092/public_html/login.php on line 26 Username doesn't exist 

我知道该用户名不存在,但是这不是问题,我经过测试,问题是mysqli_real_escape_string没有返回字符串。

有人可以帮我吗? 在此先感谢。

+3

STOP !!!!您正在使用MySQLi,因此请使用预准备语句/绑定变量。你绝对没有任何借口 – 2014-09-23 14:10:42

+1

你的代码逻辑也是__double__引用字符串值,一旦在check_input()函数中,一次在SQL查询语句中,这会给你一个无效的SQL语句.....这就是你的另一个原因应该使用绑定变量,它会处理所有的字符串引用 – 2014-09-23 14:11:16

+0

您是否选择了数据库? – iatboy 2014-09-23 14:11:30

回答

3

您的查询呈现这样的:

FROM users WHERE username LIKE ''blarg''; 

因为:

$value = "'" . mysqli_real_escape_string($con, $value) . "'"; 

和:

LIKE '".check_input($_POST['username'])."'; 

格式不正确的查询返回false这是boolean =>mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given

附加说明:

使用准备好的语句!

(对不起马克,你得到了它第一,我没看过的评论。)

0

可能是您的查询没有获取数据表单数据库。 请执行以下步骤:

1)首先回显您的查询并直接运行到mysql并确认其获取数据。 2)如果查询工作文件,然后使用'mysqli_num_rows($ result)'函数来获取多少行收集。