Php login system bug

PHP登录系统错误

问题 (Question)

So, I have recently set up a PHP login system with my database and it works perfectly apart from one small bug. I am new to PHP so I don't fully understand what the error message is, please help me out here. The code is as follows:

<?php #admin/index.php 
           #####[make sure you put this code before any html output]#####

//connect to server
$dbc = mysqli_connect('localhost','root','') or 
       die('could not connect: '.          mysqli_connect_error());

//select db
mysqli_select_db($dbc, 'examples') or die('no     db connection');

//check if the login form has been submitted
if(isset($_POST['go'])){
   #####form submitted, check data...#####

    //step 1a: sanitise and store data into vars(storing encrypted password)
$usr = mysqli_real_escape_string($dbc,     htmlentities($_POST['u_name']));
$psw = SHA1($_POST['u_pass']) ; //using SHA1() to encrypt passwords  

    //step2: create query to check if username and password match
$q = "SELECT * FROM users WHERE name='$usr' AND pass='$psw'  ";

//step3: run the query and store result
$res = mysqli_query($dbc, $q);

//make sure we have a positive result
>>>if(mysqli_num_rows($res) == 1){<<<
     #########  LOGGING IN  ##########
    //starting a session  
            session_start();

            //creating a log SESSION VARIABLE that will persist through pages   
    $_SESSION['log'] = 'in';

    //redirecting to restricted page
    header('location:restricted.php');
} else {
            //create an error message   
    $error = 'Wrong details. Please try     again'; 
}
}//end isset go
?>

I have marked the line on which the error message is refering to with >>>Content<<<. The error message is:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /home/a7686887/public_html/index.php on line 26

Please help me out on this one and please remember that I am new to PHP so well explained answers will help out a lot. Thanks!

所以,我最近和我的数据库建立一个PHP登录系统和它完美的作品,除了一个小错误。我是新来的PHP所以我不完全理解的错误消息是什么,请帮我离开这里。代码是:

<?php #admin/index.php 
           #####[make sure you put this code before any html output]#####

//connect to server
$dbc = mysqli_connect('localhost','root','') or 
       die('could not connect: '.          mysqli_connect_error());

//select db
mysqli_select_db($dbc, 'examples') or die('no     db connection');

//check if the login form has been submitted
if(isset($_POST['go'])){
   #####form submitted, check data...#####

    //step 1a: sanitise and store data into vars(storing encrypted password)
$usr = mysqli_real_escape_string($dbc,     htmlentities($_POST['u_name']));
$psw = SHA1($_POST['u_pass']) ; //using SHA1() to encrypt passwords  

    //step2: create query to check if username and password match
$q = "SELECT * FROM users WHERE name='$usr' AND pass='$psw'  ";

//step3: run the query and store result
$res = mysqli_query($dbc, $q);

//make sure we have a positive result
>>>if(mysqli_num_rows($res) == 1){<<<
     #########  LOGGING IN  ##########
    //starting a session  
            session_start();

            //creating a log SESSION VARIABLE that will persist through pages   
    $_SESSION['log'] = 'in';

    //redirecting to restricted page
    header('location:restricted.php');
} else {
            //create an error message   
    $error = 'Wrong details. Please try     again'; 
}
}//end isset go
?>

>>Content<<<. ">我已经在这条错误信息是指与>>>含量<<<。错误消息:

警告:mysqli_num_rows()预计参数1是mysqli_result,布尔在/家/ a7686887 / public_html /是26

请帮我就这一个,请记住,我是新来的PHP很好地解释答案将帮助了很多。谢谢!

最佳答案 (Best Answer)

Use:

// $res returns a mysqli_result or bool, so check it first
if($res = mysqli_query($dbc, $q)){
    // code that depends on $res comes here
}

Your query fails.

使用

// $res returns a mysqli_result or bool, so check it first
if($res = mysqli_query($dbc, $q)){
    // code that depends on $res comes here
}

你的查询失败。

答案 (Answer) 2

You need to look at any SQL errors. Your query returns a boolean, which only happens when the query fails.

你需要看任何SQL错误。你的查询返回一个布尔值,它只发生在查询失败。

答案 (Answer) 3

Seems that there is a problem with your sql statement because $res isn't a mysqli_result.

Best to check it before using it.

if ( $res )
{
    if(mysqli_num_rows($res) == 1)
    {
        ....
    }
}

似乎是你的SQL语句的一个问题,因为既不是一个mysqli_result美元。

最好使用前检查。

if ( $res )
{
    if(mysqli_num_rows($res) == 1)
    {
        ....
    }
}

答案 (Answer) 4

You should check you $res variable, from the PHP documentation:

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

My guess is that your query is returning false and you're passing it to the mysqli_num_rows() function.

你应该检查你的$res变量,从PHP文档

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

我的猜测是,你的查询返回的是false你传递给mysqli_num_rows()功能。

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/22082464