【问题标题】:PHP Function and encoding result into JSONPHP函数和编码结果为JSON
【发布时间】:2019-08-12 12:36:10
【问题描述】:

我正在尝试调用一个函数并将结果传递到 PHP 中的 json 对象中。除功能部分外,一切正常。我在字符 1 处不断收到输入结束错误。

这是给我带来麻烦的一行

$ImageCount =  getCount($fullName);
$response["count"] = $ImageCount;

这是完整的文件:

<?php
$response = array();

include 'db/db_connect.php';
include 'functions.php';

$inputJSON = file_get_contents('php://input');

//Get the input request parameters
$input = json_decode($inputJSON, TRUE); //convert JSON into array

//Check for Mandatory parameters
if(isset($input['username']) && isset($input['password'])){
    $username = $input['username'];
    $password = $input['password'];
    $query    = "SELECT full_name,password_hash, salt FROM member WHERE username = ?";

    if($stmt = $con->prepare($query)){
        $stmt->bind_param("s",$username);
        $stmt->execute();
        $stmt->bind_result($fullName,$passwordHashDB,$salt);
        if($stmt->fetch()){

            //Validate the password
            if(password_verify(concatPasswordWithSalt($password,$salt),$passwordHashDB)){
            $ImageCount =  getCount($fullName);
            $response["status"] = 0;
            $response["message"] = "Login successful";
            $response["full_name"] = $fullName;
            $response["count"] = $ImageCount;
        }else{
            $response["status"] = 1;
            $response["message"] = "Invalid username and password combination";
        }
    }else{
        $response["status"] = 1;
        $response["message"] = "Invalid username and password combination";
    }

    $stmt->close();
    }
}else{
    $response["status"] = 2;
    $response["message"] = "Missing mandatory parameters";
}
//Display the JSON response
echo json_encode($response);
?>

我的 getCount() 函数代码:

function getCount($fname){
    global $count;
    $path = 'MobileApp/Images/'; 
    $slash = '/';
    $fullpath = $path.$fname.$slash;
    //echo $fullpath;
    $fi = new FilesystemIterator($fullpath, FilesystemIterator::SKIP_DOTS);
    $count = (iterator_count($fi));
    return $count;
}

【问题讨论】:

  • 你是在php端还是android端出错?
  • @Exception PHP 端。如果我删除麻烦的代码行,一切都会完美。但是一旦我添加它。我收到错误(错误作为“结果”传递给 android,我在 logcat 上显示)。
  • 你在说什么麻烦线?
  • @B001ᛦ 我没看到。我在使用 wifi 或移动数据时没有问题。另外我的问题是使用该函数获取值并将其以 JSON 传递。我的 JSON 工作正常。因为我可以在 $count 中传递我想要的任何值。但是,当我调用函数 getCount() 时,我得到了错误。另外我已经单独测试了这个功能,它工作正常。
  • @Exception $ImageCount = getCount($fullName);

标签: php android json parsing


【解决方案1】:

我认为问题出在 getCount() 函数上。

PHP 支持 count() 获取数组中的元素个数。

count

(PHP 4, PHP 5, PHP 7)

count — Count all elements in an array, or something in an object
Description
count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] ) : int

请查看https://www.php.net/manual/en/function.count.php

【讨论】:

  • 我已更新我的问题以包含 getCount() 函数
  • 您已将函数定义为getImagesCount(),但您已编写getCount()...更改并重新测试
  • 那是我的错误。我错误地复制了旧名称。确保我使用正确的名称调用函数。
  • 我认为这只是你调用不存在的函数的问题
  • 我正在调用 getCount()。我的函数被命名为 getCount()。能详细点吗?
猜你喜欢
  • 2013-07-20
  • 1970-01-01
  • 1970-01-01
  • 2010-09-27
  • 1970-01-01
  • 1970-01-01
  • 2013-10-27
  • 1970-01-01
  • 2011-11-24
相关资源
最近更新 更多