为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
潇湘沐 2021-05-07 17:23:17
当尝试使用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 APIfunction 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 43if (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);//editvar_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应用程序,以将密码而不是固定密码发布到数据库,因为它正在创建错误。
查看完整描述

2 回答

?
料青山看我应如是

TA贡献1772条经验 获得超8个赞

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

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

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

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


查看完整回答
反对 回复 2021-05-28
?
红颜莎娜

TA贡献1842条经验 获得超12个赞

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');

}


}


查看完整回答
反对 回复 2021-05-28
  • 2 回答
  • 0 关注
  • 153 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信