数据库查询返回:检查参数是数组还是对象

时间:2019-05-05 04:06:33

标签: php mysql sql

当尝试使用php和mysql登录此应用程序时,将显示该应用程序中使用的Authorization failed错误。读取php_error.log时显示:

  

[2019年5月5日03:31:51 UTC] PHP注意:未定义的索引:导致   /Applications/MAMP/htdocs/iReporter/api.php,第43行[2019年5月5日   03:31:51 UTC] PHP警告:count():参数必须为数组或   实现Countable in的对象   /Applications/MAMP/htdocs/iReporter/api.php,第43行

登录功能

//login API
function login($user, $pass) {

// try to match a row in the "login" table for the given username and password
$result = query("SELECT IdUser, username FROM login WHERE username='%s' AND pass='%s' limit 1", $user, $pass);

//Line 43
if (count($result['result'])>0) {
    // a row was found in the database for username/pass combination
    // save a simple flag in the user session, so the server remembers that the user is authorized
    $_SESSION['IdUser'] = $result['result'][0]['IdUser'];

    // print out the JSON of the user data to the iPhone app; it looks like this:
    // {IdUser:1, username: "Name"}
    print json_encode($result);
//edit
var_dump($result);

} else {
    // no matching username/password was found in the login table
    errorJson('Authorization failed');
}

}

此应用程序用于让用户无缝登录,但突然停止工作,而无需对代码进行任何更改。即使未更改的备份文件也收到相同的错误。如何更改代码以允许用户成功登录?

更新:index.php。应用程序访问功能的主文件

switch ($_POST['command']) {
case "login":
    login($_POST['username'], $_POST['password']); 
    break;

已解决

已将iOS应用更改为在创建错误时将密码而不是固定密码发布到数据库中。

3 个答案:

答案 0 :(得分:2)

基于该错误,您没有在result变量中获得$result值。要检查这一点,您可以使用:

if (isset($result['result']) && count($result['result']) > 0)

首先检查该值是否已设置。

您还将要调查为什么无法从数据库中获得预期结果。为此,您将需要查看$result变量中返回的内容,并查找数据库查询可能返回的任何错误。

答案 1 :(得分:1)

除了is_array之外,您还可以添加isset(),否则访问被拒绝。

function login($user, $pass)
{

// try to match a row in the "login" table for the given username and password
    $result = query("SELECT IdUser, username FROM login WHERE username='%s' AND pass='%s' limit 1", $user, $pass);

    if (is_array($result) && isset($result) && count($result) > 0) {
        if (isset($result['result'])) {
            // a row was found in the database for username/pass combination
            // save a simple flag in the user session, so the server remembers that the user is authorized
            $_SESSION['IdUser'] = $result['result'][0]['IdUser'];

            // print out the JSON of the user data to the iPhone app; it looks like this:
            // {IdUser:1, username: "Name"}
            print json_encode($result);
        }else{
            errorJson('Sorry! There is a problem with $result["result"]!'); 
        }
    } else {
        // no matching username/password was found in the login table
        errorJson('Authorization failed');
    }

}

由于您的数组不是数组且不可计数,因此错误返回。

答案 2 :(得分:1)

function login($user, $pass) {

// try to match a row in the "login" table for the given username and password
$result = query("SELECT IdUser, username FROM login WHERE username='%s' AND pass='%s' limit 1", $user, $pass);

$rows=$result->num_rows;
//Line 43

if ($rows>0) {
    // a row was found in the database for username/pass combination
    // save a simple flag in the user session, so the server remembers that the user is authorized
    $_SESSION['IdUser'] = $result['result'][0]['IdUser'];

    // print out the JSON of the user data to the iPhone app; it looks like this:
    // {IdUser:1, username: "Name"}
    print json_encode($result);
} else {
    // no matching username/password was found in the login table
    errorJson('Authorization failed');
}

}