0

I have maybe a really simple problem for you but it's making me crazy.

In my index.php I call my userHandler to retrieve data from MySQL in this first line of code but whatever I do the $result stays null.

When debugging, I can clearly see that the MySQL returns one row and its not null.

$result = $userHandler->getUserByPhone($phoneNumber);

if ($result != NULL) {
}

Here is the method that returns result:

public function getUserByPhone($Phone) {
    $stmt = $this->conn->prepare("SELECT user.* FROM user");
    $stmt->execute();
    $result = $stmt->get_result();
    $num_of_rows = $result->num_rows;
    $stmt->store_result();
    $stmt->close();
    return $result;
}

And here is the screenshot of function enter image description here

and it returns null enter image description here

Edit: Ok i tought that after closing the statement the data would be lost. and thats true if i still want to return the data of the statement. but before closing i stored the data in a variable. and then close it. I checked in debug after closing the variable still has the data. enter image description here

Then i thought maybe the variable will be set null too (after all im a noob in php i can think none sense :D ) so i only returned True. But its still null in receiving end.

6
  • 1
    What are you trying to return? You'll likely want to get the data out of $result before you close the statement. Commented Jul 12, 2018 at 21:00
  • 1
    If $result is not null inside the function, there's no way ot could be null outside. There must be something else going on that you're not showing. Commented Jul 12, 2018 at 21:03
  • 1
    php.net/manual/en/mysqli-stmt.close.php this closes the handle and any unread items. So this result could be null, as they have not been read. Just try commenting out $stmt->close(); Commented Jul 12, 2018 at 21:10
  • @ShaunForsyth but its not an issue in other functions. and i see clearly in debug even after closing the statement the result that i stored in variable $result is not empty. I even checked with a return True; instead of return $result. its still Null. Commented Jul 12, 2018 at 21:15
  • Your debugger shows you're on the stmt_close() line, usually you need to be on the next line to show the result of that call. Commented Jul 12, 2018 at 21:18

1 Answer 1

2

You'd need to pull the data you need out of $result prior to closing the statement. Closing the statement will clear the statement and the related result set, rendering the value null.

Since getUserByPhone should seemingly return a user, I'd suggest that you get the user data out of the result and return that rather than closing the statement then trying to return the mysqli result object.

Something like:

$result = $stmt->get_result();
$user = $result->fetch_assoc();
$stmt->close(); 
return $user;
Sign up to request clarification or add additional context in comments.

1 Comment

No I checked again. I still get null with thiw code

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.