i was working on an api base on a tutorial which it was getting token from user to recognize that user is admin or not.
so it's just a question about is this really matter to use
+1xxx-xxxx-xxx
Or
%2B1xxx-xxxx-xxx
When i've tested my api it's returning null if i input wrong data in isServerToken, and if i use right one it's gonna return right value. but if i use +
instead of [%2B]
it's gonna get me this error :
<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
I'll comment 389 line in below
No matter if i use right or wrong input for isServerToken. It's gonna say top error.
I'm asking this because i'm getting error from my android about
java.langillegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
so i'm considering every possibility about what is making this error happen. here are the codes for PHP API
. In tutorial instructor used get_result, but I've changed it to bind_result because it won't work on my online host.
here are the codes :
// 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.
}
And app calling from this part :
//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);
}
I want to make sure when use register with +xxxx-xxxx-xxx number is this making my api say top error or not, because as i said it's working fine with %2Bxxxx-xxxx-xxx.
Also number is saving with +
in database.
When I've line to this base on suggestions things got opposite now +
work and 2%B
will be null.
$token = $db->getToken(urlencode($userPhone),$isServerToken);
Thanks