我正在使用这个适用于安卓应用程序的 PHP API 来获取令牌以根据服务器和用户[手机]发送通知。 问题是我在应用程序中遇到错误,只有我的 API 的这一部分出现错误。 哪个错误是
<br />
<b>Notice</b>: Undefined variable: token in <b>/home/meskand1/public_html/pasargad-drinkshop/db_functions.php</b> on line <b>390</b><br />
null
我在下面评论了第 390 行和函数代码。
代码在get_Result&fetch_assoc上运行良好,但由于在线主机不支持mysqlnd,我被迫更改了这一点。 谁能解释错误在哪里发生以及为什么? 在教程中get_Result讲师返回$token,现在使用我的代码出现错误。
下面是函数中的代码:
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($a,$b,$c);
while ($stmt->fetch()){
$token[] = ['phone' => $a, 'token' => $b, 'isServerToken' => $c];
}
$stmt->close();
return $token; // => This is line 390
}
叫
if(isset($_POST['phone']) && isset($_POST['isServerToken'])){
$userPhone = $_POST['phone'];
$isServerToken = $_POST['isServerToken'];
$token = $db->getToken(urlencode($userPhone),$isServerToken);
echo json_encode($token);
} else {
$response = "Required parameter (phone , isServerToken) is missing!";
echo json_encode($response);
}
如果您的 SQL 不匹配任何数据,则$token
是未定义的,因为它仅在循环中设置。 您至少应该在循环之前初始化它,以防万一......
$stmt->bind_result($a,$b,$c);
$token = array(); // Initialise
while ($stmt->fetch())
{
$token[] = ['phone' => $a, 'token' => $b, 'isServerToken' => $c];
}
$stmt->close();
return $token;