Note that there are some explanatory texts on larger screens.

plurals
  1. POMysqli code to fetch one result and return it
    text
    copied!<p>I'm new to php. I have a login site where users login with their emails. Each is assigned an auto-increment user_id. I want to fetch the associated user_id from the database and store it in the session so that when they submit content, their user_id can be gotten from the session and submitted with the content. However, my "get_user_id" function is not working to look up the user id from their email address. I've tried using all sorts of mysqli commands from fetch_row, get_result, bind_result, and nothing will return the user_id.</p> <pre><code> function get_user_id($email) { //$email = mysqli_real_escape_string($this-&gt;conn, $email); $query = "SELECT id from users where email=?"; if ($stmt = $this-&gt;conn-&gt;prepare($query)){ $stmt-&gt;bind_param('s',$email); if($result = $stmt-&gt;execute()){ $row=$result-&gt;fetch_row(); return $row['id']; } else return "no ID returned."; } } </code></pre> <p>Edit: the error message I am getting now is: <code>Fatal error: Call to a member function fetch_row() on a non-object</code></p> <p>Update: This is what worked - thanks RikkusRukkus:</p> <pre><code> function get_user_id($email) { //$email = mysqli_real_escape_string($this-&gt;conn, $email); $query = "SELECT id from users where email=?"; if ($stmt = $this-&gt;conn-&gt;prepare($query)){ $stmt-&gt;bind_param('s',$email); if($result = $stmt-&gt;execute()){ $stmt-&gt;bind_result($user_id); $stmt-&gt;fetch(); return $user_id; } else return "no ID returned."; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload