2

I have a function which works like this

public function get_user_by_email($email)
{
    $stmt = $this->con->prepare("SELECT * FROM `user` WHERE `email` = ? ");
    $stmt->bind_param("s", $email);
    $stmt->execute();

    return $stmt->get_result()->fetch_assoc();
}

The problem is whenever I call this function with any email(whether it existed on the database or not) it throws internal server error 500. I'm using a live hosting server and Postman to test this. Can someone help?

Bot
  • 41
  • 7
  • Check your `$email` is a valid string to fit for `s` in the `bind_param()` call. – Touhid Jul 25 '20 at 09:16
  • I did that's not the issue. Also, I found out this error only occurs on the live server and not when I test it on my local server(xampp server) – Bot Jul 25 '20 at 09:18
  • 1
    Then it might be a missing part to your DB connection or table-existence. You should look for relevant text-message around that 500 error code, Postman usually shows them for standard frameworks. Otherwise, you can `echo` a progress-message after each executed line to find out which line is causing the 500. – Touhid Jul 25 '20 at 09:22
  • The return statement is causing this error the `get_result();` or maybe the `fetch_assoc()` part. If I just `return 0` or `return 1` it works fine. I just don't get the intended results then. – Bot Jul 25 '20 at 09:25
  • Does this answer your question? [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Dharman Jul 25 '20 at 12:18

2 Answers2

1

Try debugging with the following code:

public function get_user_by_email($email)
{
    $stmt = $this->con->prepare("SELECT * FROM `dbname.user` WHERE `email` = ? ");
    $stmt->bind_param("s", $email);
    $stmt->execute();

    $result = $stmt->get_result();
    echo "Result Object: ";
    echo $result;  // If it's OK, it should print "Object ( ... )"

    $arr = $result->fetch_assoc();
    echo "Fetched Object: ";
    echo $arr; // If it's OK, then it'd print "Array ( ... )"

    return $arr;
}
Touhid
  • 731
  • 1
  • 10
  • 25
1

I experienced this error as well. It is coming from get_result() -> https://www.php.net/manual/en/mysqli-stmt.get-result.php

I built an app using JS, PHP and MySQL. Everything worked well locally but when I uploaded it to a shared server, I start getting 500 Internal Server error (The server host might have thrown 500 error instead of displaying Fatal Error on screen). This got fixed when I disabled mysqli and enabled nd_mysqli from my hosting's PHP extensions.

Ely
  • 11
  • 4
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/30485617) – user11717481 Dec 02 '21 at 19:39