0

I'm trying to retrieve the "id" from MYSQL table named user where the email is a certain value, but I cannot get it to work. The code I'm using is

$sql="SELECT id FROM user WHERE email=$email";
$result=$conn->query($sql);
while($row=$result->fetch_assoc()){ 
    $id=$row["id"];
}

When I look at the erro log, it says the following:

PHP Fatal error: Call to a member function fetch_assoc() on a non-object

How can I get this to work?

AmyShmil
  • 84
  • 7
  • Write $email between single quotes, and i strongly advice you to use pdo. – mim. Jun 30 '15 at 20:36
  • 1
    Your code does not contain any error handling at all. What do you expect happens if the query runs into an error? You have to _check_ if the query actually succeeds and returns a usable result instead of blindly using it. – arkascha Jun 30 '15 at 20:38
  • "PDO::query() returns a PDOStatement object, or FALSE on failure." You must handle the false case. Do not assume that the result will be an object. – Shashank Jun 30 '15 at 20:41
  • Don't forget to use prepared statements. You can't just slap stuff like `$email` in your query and not face [consequences](http://bobby-tables.com/). – tadman Jun 30 '15 at 21:31

0 Answers0