mysqli_query给出空值

时间:2014-09-21 17:05:55

标签: php mysql sql mysqli

我为我的网站创建了一个登录系统。 我遇到了我的mysqli_query为空的问题:

代码:$r = mysqli_query($dbc, $q); returns empty.

我认为问题在于我传递的sql语言:

$q = "SELECT userid, fname, lname FROM User WHERE email='$e' AND pass=SHA1('$p')";

如果我使用我实际从表单中获取的数据执行上面的sql,那么我将得到我正在寻找的结果。所以我认为它的php和sql结合起来会出错。

if(empty($errors)){
    $q = "SELECT userid, fname, lname FROM User WHERE email='$e' AND pass=SHA1('$p')";

    $r = mysqli_query($dbc, $q);
    //Check for right data
    $errors[] = $e;
    $errors[] = $p;
    //Check if connection is up
    if($dbc != null){
        $errors[] = "Connection up";
    }
    //Check query
    if(is_null($r)){
        $errors[] = "Query is null";
    }
    //Check query for being empty
    if(empty($r)){
        $errors[] = "Its empty";
    }
    if( mysqli_num_rows($r) == 1){
        $row = mysqli_fetch_array($r, MYSQLI_ASSOC);
        return array(true, $row);
    }else{
        $errors[] = "Email address and password not found.";
    }
    return array(false, $errors);

}

1 个答案:

答案 0 :(得分:1)

你可以试试这个

if(empty($errors)){
    $p = sha1($p);
    $q = "SELECT userid, fname, lname FROM User WHERE email = '$e' AND pass = '$p'";

    $r = mysqli_query($dbc, $q);
    if(!$r){
        die(mysqli_error($dbc));
    }

    //Check for right data
    $errors[] = $e;
    $errors[] = $p;
    //Check if connection is up
    if($dbc != null){
        $errors[] = "Connection up";
    }
    //Check query
    if(is_null($r)){
        $errors[] = "Query is null";
    }
    //Check query for being empty
    if(empty($r)){
        $errors[] = "Its empty";
    }
    if( mysqli_num_rows($r) > 0){
        $row = mysqli_fetch_assoc($r);
        return array(true, $row['userid'], $row['fname'], $row['lname']);
    }else{
        $errors[] = "Email address and password not found.";
    }
    return array_merge(array(true), $errors);
}