Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 数据库查询返回:检查参数是数组还是对象_Php_Mysql_Sql - Fatal编程技术网

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

Php 数据库查询返回:检查参数是数组还是对象,php,mysql,sql,Php,Mysql,Sql,当尝试使用php和mysql登录此应用程序时,将显示应用程序中使用的授权失败错误。读取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():参数必须是数组或 对象,该对象在 /第43行的Applications/MAMP/htdocs/iReporter/api.p

当尝试使用php和mysql登录此应用程序时,将显示应用程序中使用的
授权失败
错误。读取
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():参数必须是数组或 对象,该对象在 /第43行的Applications/MAMP/htdocs/iReporter/api.php

登录功能

//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应用程序更改为向数据库发布密码,而不是附加密码,因为它正在创建错误

根据错误,您没有在
$result
变量中获得
结果
值。要检查这一点,您可以使用:

if (isset($result['result']) && count($result['result']) > 0)
它首先检查值是否已设置


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

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

if (isset($result['result']) && count($result['result']) > 0)
它首先检查值是否已设置


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

除了
isset()
之外,还可以添加
is_数组
,否则访问将被拒绝

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

}

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

除了
isset()
,您还可以添加一个
is_数组
,否则访问被拒绝

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

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

}


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

你能检查一下它是否从数据库中提取数据吗。确保它已连接到MySQLdatabases@Sathiraumesh是的。在尝试注册功能时,如果尝试使用现有用户名,则成功显示已使用
用户名
错误。它还成功地将应用程序数据库中的图片显示在应用程序的主屏幕上;如果($rows>0){}谢谢。我应该在哪里将其添加到代码中?您可以检查它是否从数据库中获取数据。确保它已连接到MySQLdatabases@Sathiraumesh是的。在尝试注册功能时,如果尝试使用现有用户名,则成功显示已使用
用户名
错误。它还成功地将应用程序数据库中的图片显示在应用程序的主屏幕上;如果($rows>0){}谢谢。我应该在哪里将其添加到代码中?谢谢您的回复。现在,错误日志在第44行的/Applications/MAMP/htdocs/iReporter/api.PHP中显示
[05-May-2019 05:05:26 UTC]PHP解析错误:语法错误,意外的“计数”(T_字符串)。错误日志显示
[05-May-2019 05:33:16 UTC]PHP注意:未定义索引:当
变量转储($result)时,第44行的/Applications/MAMP/htdocs/iReporter/api.PHP中出现结果
被添加到登录函数中,就像编辑的问题一样,当试图通过browserYeah访问index.php文件时,在浏览器中看不到任何输出,它以前使用的是
$result['result']
,它只是突然停止。当然是从2012年开始的。资料来源:下载在底部Ok。我看到
“对不起!$result[“result”]有问题错误。再次感谢。我会看看能做些什么…谢谢你的回复。现在,错误日志在第44行的/Applications/MAMP/htdocs/iReporter/api.PHP中显示
[05-May-2019 05:05:26 UTC]PHP解析错误:语法错误,意外的“计数”(T_字符串)。错误日志显示
[05-May-2019 05:33:16 UTC]PHP注意:未定义索引:当
变量转储($result)时,第44行的/Applications/MAMP/htdocs/iReporter/api.PHP中出现结果
被添加到登录函数中,就像编辑的问题一样,当试图通过browserYeah访问index.php文件时,在浏览器中看不到任何输出,它以前使用的是
$result['result']
,它只是突然停止。当然是从2012年开始的。资料来源:下载在底部Ok。我看到
“对不起!$result[“result”]有问题错误。再次感谢。我会看看能做些什么…谢谢你的回复。奇怪的是,它仍然显示
授权失败
错误,并且没有任何输出到错误日志谢谢您的回复。奇怪的是,它仍然显示
授权失败
错误,并且没有任何输出到错误日志谢谢您的回复。现在,错误日志显示“[05-May-2019 05:18:23 UTC]PHP注意:尝试在第44行的/Applications/MAMP/htdocs/iReporter/api.PHP中获取非对象的属性'num_rows'`您能检查在执行查询后是否填充了$result变量吗;ini设置(“显示错误”,1)已添加到索引文件中。当应用程序运行时,浏览器输出类似于
array(0)
。但是现在,当尝试运行带有错误报告语句的应用程序时,将显示(200-299)中的预期状态代码,Get 500
。使用登录功能的位置更新了问题谢谢您的回答。现在错误日志显示“[05-May-2019 05:18
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');
}

}