1

I'm using this PHP API for android app to get token to send notification base on server and user[Phone]. problem is i'm getting error in app and only this part of my api is getting errors. which error is

<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

I've commented line 390 and function code below.

Code was working fine with get_Result & fetch_assoc, but because online host didn't supported mysqlnd, i've forced to change this. can anyone explain where is mistake happen and why? in tutorial with get_Result instructor returned $token, now with my code this is getting errors.

Here's the code in function:

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
}

Call

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

1 Answer 1

1

If your SQL doesn't match any data, $token is undefined as it is only ever set in the loop. You should at least initialise it before the loop just in case...

$stmt->bind_result($a,$b,$c);
$token = array();    // Initialise
while ($stmt->fetch())
{
    $token[] = ['phone' => $a, 'token' => $b, 'isServerToken' => $c];
}
$stmt->close();
return $token; 
Sign up to request clarification or add additional context in comments.

4 Comments

omg, u don't belive man if i just tell u i find it before i saw this answer, but this answer gimme best Confirmation, i've looked for another example and changed it to another method, in this method i've initialized like u said, then i've saw your answer and i've found best place to put it. Thanks mate.
@PaulSpiegel any e.g. ? can u answer it base on top code? i've learned so many base on all these ideas, i'll be grateful if u make another answer base on what u've suggested.
@MohammadEskandari - I removed my comment with fetch_all() since the manual says: "Available only with mysqlnd". However - ->fetch_assoc() in a loop might work for you. I don't see any use for bind_result() in your case.
@PaulSpiegel thanks for your response, right now i'm using sharing host, which hosting service said it's premium :D, but in months i'll go for vps so i can fix mysqlnd problem. anyway, u already gimme a clue about another thing and it's amazing, if u could answer that example base on top code i would be happy to test it on local which is working without any problem with mysqlnd. thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.