mysql 在数字前使用{%2B)代替(+)有何区别?

zbsbpyhn  于 5个月前  发布在  Mysql
关注(0)|答案(1)|浏览(35)

我正在一个API的基础上的教程,它是从用户获得令牌,以认识到用户是管理员或没有。
所以这只是一个问题,这真的对用途:很重要吗?

+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使用正确或错误的输入,它都会显示top error。
我问这个是因为我的机器人出错了,
java.langillegalStateException:应为开始_OBLOG,但在第1行第2列路径$处为BEGIN_ARRAY
所以我在考虑是什么导致这个错误发生的每一种可能性。这里是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.
}


这部分的app调用:

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

evrscar2

evrscar21#

getToken的第二个函数声明中,如果while结构没有被循环,则永远不会创建变量$token(因为您只在循环中声明$token)。
因此,可能的结果是变量$token在返回$token时不存在(这就是你正在经历的)。换句话说,在你的例子中没有任何结果被循环。
另外:加号在JSON:https://stackoverflow.com/a/1374456/5682311中不是很好的匹配
在解析为json_encode之前,先在phoneber(或任何值)上使用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; 
}

字符串

相关问题