mysqli_num_rows()期望参数1为mysqli_result,布尔给定错误

时间:2015-10-20 10:12:59

标签: php html mysqli

我正在尝试使用php创建一个简单的登录系统。但我一直在走同样的问题。我按照几个教程,仍然没有工作。我已经在谷歌上搜索了这个问题。但我似乎无法找到答案。

$check_user = mysqli_num_rows($run_user);

这是第17行并导致错误。

<?php
$connection = mysqli_connect("localhost", "root", "", "summacollege");

if (mysqli_connect_errno())
{
 echo "something went wrong with the connection" . mysqli_connect_error();
}

if(isset($_POST['submit'])){
$username = mysqli_real_escape_string($connection, $_POST['username']);
$password = mysqli_real_escape_string($connection, $_POST['password']);

$sel_user = "SELECT * FROM login WHERE username='$username' AND password='$password'";
$run_user = mysqli_query($connection, $sel_user);
$check_user = mysqli_num_rows($run_user);

if($check_user > 0){
    $_SESSION['username']= $username;
    echo "<script>window.open('index.php', '_self')</script>";
}
else{
    echo "gebruikernaam of wachtwoord is incorrect.";
}
}
?>

它不断给我这个错误代码:

  

警告:mysqli_num_rows()期望参数1为mysqli_result,   布尔值在C:\ wamp \ www \ oefening \ includes \ checklogin.php中给出   17

5 个答案:

答案 0 :(得分:0)

问题是您的查询已返回false

$sel_user = "SELECT * FROM login WHERE username='$username' AND password='$password'";
$run_user = mysqli_query($connection, $sel_user);
if (!$run_user) {
        die(mysqli_error($connection));
    }else{
    //write all code
    }

答案 1 :(得分:0)

问题的根源是mysqli_query返回mixed

  

如果成功则为TRUE;如果出现错误则为FALSE。对于SELECT,SHOW,DESCRIBE或EXPLAIN,mysqli_query()将返回一个结果对象。

此处获得boolean的原因是因为您的SELECT查询失败了。

$run_user来电之前检查mysqli_num_rows($run_user)

if (!$run_user) {
    die("Select failed");
}

一些use examples

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* Create table doesn't return a resultset */
if ($mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
    printf("Table myCity successfully created.\n");
}

/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) {
    printf("Select returned %d rows.\n", $result->num_rows);

    /* free result set */
    $result->close();
}

/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result = $mysqli->query("SELECT * FROM City", MYSQLI_USE_RESULT)) {

    /* Note, that we can't execute any functions which interact with the
       server until result set was closed. All calls will return an 
       'out of sync' error */
    if (!$mysqli->query("SET @a:='this will not work'")) {
        printf("Error: %s\n", $mysqli->error);
    }
    $result->close();
}

$mysqli->close();
?>

答案 2 :(得分:0)

查询错误。尝试删除&#39;&#39;在您的查询中。那就是

$sel_user = "SELECT * FROM login WHERE username=$username AND password=$password";

答案 3 :(得分:0)

尝试更改代码的以下部分 从这个:

$check_user = mysqli_num_rows($run_user); 
if($check_user > 0){
$_SESSION['username']= $username;
echo "<script>window.open('index.php', '_self')</script>";
}

对此:

if( !run_user||mysqli_num_rows($run_user) > 0){
$_SESSION['username']= $username;
echo "<script>window.open('index.php', '_self')</script>";
}

答案 4 :(得分:0)

嘿,从mysql检查数据库名称或正确检查花括号 或尝试这个

<?php
$connection = mysqli_connect("localhost", "root", "", "summacollege");

if (mysqli_connect_errno())
{
 echo "something went wrong with the connection" . mysqli_connect_error();
}
    if(isset($_POST['submit'])){
    $username = mysqli_real_escape_string($connection, $_POST['username']);
    $password = mysqli_real_escape_string($connection, $_POST['password']);

    $sel_user = "SELECT * FROM login";
    $run_user = mysqli_query($connection, $sel_user);
   if(mysqli_num_rows($run_user) > 0){
        $row = mysqli_fetch_array($run_user);
        $db_username = $row['username'];//copy the column name from database $row['colname'];
        $db_password = $row['password'];
        if($username == $db_username && $password == $db_password){
           header('Location:index.php');
           //to use this function add ob_start(); at top
         }
    }
else{
        echo "gebruikernaam of wachtwoord is incorrect.";
    }
    }
    ?>

检查它将起作用的代码。我已经实现了