php - 在数字前使用 {%2B) 而不是 (+) 有什么区别?

标签 php android mysql api retrofit2

我在一个基于教程的 api 上工作,它从用户那里获取 token 以识别用户是否是管理员。

所以这只是一个问题,这个问题真的很重要吗

+1xxx-xxxx-xxx

或者

%2B1xxx-xxxx-xxx

当我测试我的 api 时,如果我在 isServerToken 中输入错误数据,它会返回 null,如果我使用正确的数据,它会返回正确的值。但是如果我使用 + 而不是 [%2B] 它会给我这个错误:

<br />
<b>Notice</b>:  Undefined variable: token in <b>/h**e/m***/pu**tml/***/db_functions.php</b> on line <b>389</b><br />
null

我会在下面评论389行

无论我对 isServerToken 使用正确还是错误的输入。它会说最高错误。

我问这个是因为我从我的 android 中收到关于

的错误
java.langillegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

所以我正在考虑导致此错误发生的所有可能性。以下是 PHP API 的代码。在教程中,讲师使用了 get_result,但我已将其更改为 bind_result,因为它在我的在线主机上不起作用。

代码如下:

// Instructer Code
public function getToken($phone,$isServerToken)
{
    $stmt = $this->conn->prepare("SELECT * FROM `token` WHERE phone=? AND isServerToken=?") or die ($this->conn->error);
    $stmt->bind_param("ss",$phone,$isServerToken);
    $result = $stmt->execute();
    $token = $stmt->get_result()->fetch_assoc();
    $stmt->close();
    return $token;
}

// What i've changed to Bind
public function getToken($phone,$isServerToken)
{
    $stmt = $this->conn->prepare("SELECT phone, token, isServerToken FROM `token` WHERE phone=? AND isServerToken=?") or die ($this->conn->error);
    $stmt->bind_param("ss",$phone,$isServerToken);
    $result = $stmt->execute();
    $stmt->bind_result($arr['phone'], $arr['token'], $arr['isServerToken']);
    while ($stmt->fetch())
    {
        $token[] = $arr;
    }
    $stmt->close();
    return $token; => This is where `Undefined variable: token in` happen.
}

应用从这部分调用:

//Only instructor code, i didn't changed anything this part
if(isset($_POST['phone']) && isset($_POST['isServerToken']))
{
$userPhone = $_POST['phone'];
$isServerToken = $_POST['isServerToken'];

$token = $db->getToken($userPhone,$isServerToken);

echo json_encode($token);


}
else{
$response = "Required parameter (phone , isServerToken) is missing!";
echo json_encode($response);
}

我想确定当使用 +xxxx-xxxx-xxx 号码注册时是否会导致我的 api 报错,因为正如我所说,它在 %2Bxxxx-xxxx-xxx 下工作正常。

此外,数字正在使用 + 保存在数据库中。

当我根据建议与此一致时,事情变得相反,现在 + 起作用并且 2%B 将为空。 $token = $db->getToken(urlencode($userPhone),$isServerToken);

谢谢

最佳答案

getToken 的第二个函数声明中,如果 while 构造没有被循环,变量 $token 将永远不会被创建(因为您在循环内在线声明 $token)。

因此,可能的结果是变量 $token 在返回 $token 时不存在(这就是您遇到的情况)。换句话说,在您的案例中没有循环任何结果。

另外:加号在 JSON 中不是很好的匹配: https://stackoverflow.com/a/1374456/5682311

在将电话号码(或任何值)解析为 json_encode 之前,对电话号码(或任何值)使用 php 的 urlencode 函数,听起来像是解决方案(它将转换 + 登录 %2B)

编辑:

public function getToken($phone,$isServerToken)
{
    $stmt = $this->conn->prepare("SELECT phone, token, isServerToken FROM `token` WHERE phone=? AND isServerToken=?") or die ($this->conn->error);
    $stmt->bind_param("ss",$phone,$isServerToken);
    $result = $stmt->execute();
    $stmt->bind_result($arr['phone'], $arr['token'], $arr['isServerToken']);

    $token = array(); // <- added this line, to be sure variable $token always exists
    while ($stmt->fetch())
    {
        $token[] = array_map('urlencode', $arr ); // <- urlencode all values within $arr
    }
    $stmt->close();
    return $token; 
}

关于php - 在数字前使用 {%2B) 而不是 (+) 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51809844/

相关文章:

php - Mod Rewrite Htaccess for Fake sub Directories and directory

android - 当编译器没有给出任何错误时,为什么 RecyclerView 不工作?

java - 使用 Okhttp3 的 OPTIONS/HEAD REST API 请求

android - Facebook Android sdk 游戏请求对话框显示 "Game Requests are only available to games"

mysql - 删除 MySQL 中的重复行

javascript - Bootstrap Selectpicker 不适用于全局模式

php - Mimik 调平算法

php - 在 php foreach 中增加一个值?

java - 使用sql在java中为用户帐户创建一个表

mysql - 连接三表即可得到贷款金额、员工总出勤率和工资