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

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


[05.05.2019 03:31:51 UTC] PHP注意:未定义的索引:结果在第43行的/Applications/MAMP/htdocs/iReporter/api.php中[05-May-2019 03:31:51 UTC] PHP警告:count():参数必须是在第43行的/Applications/MAMP/htdocs/iReporter/api.php中实现Countable的数组或对象


登录功能


//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应用程序,以将密码而不是固定密码发布到数据库,因为它正在创建错误。


潇湘沐
浏览 154回答 2
2回答

料青山看我应如是

基于该错误,您没有result在$result变量中获取该值。要检查这一点,您可以使用:if (isset($result['result']) && count($result['result']) > 0)首先检查该值是否已设置。您还需要调查为什么无法从数据库中获得预期的结果。为此,您将需要查看$result变量中返回的内容,并查找数据库查询可能返回的任何错误。

红颜莎娜

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 43if ($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');}}
打开App,查看更多内容
随时随地看视频慕课网APP