I attempt to get the result of a very simple query with the function query but nothing appears. If I execute the query in PHPMyAdmin, with the same data, I have a result.
There is my code :
$sql = "SELECT * FROM users WHERE email='$email'"; $response = $conn->query($conn, $sql);
The $conn variable is correct, I did an Insert with that.
$response is null. I can do an echo and there is nothing.
What can I do to solve this problem ? What can I check ?
Thank you very much.
Advertisement
Answer
You don’t need to pass connection in query.
Solution:
$sql = "SELECT * FROM users WHERE email='$email'"; $response = $conn->query($sql); while($res = $response->fetch_array()){ $name=$res['nameofuser']; //just an example } echo $name;
Real solution (prepare stmt):
$sql = "SELECT * FROM users WHERE email=?"; $response = $conn->prepare($sql); $response->bind_param('s',$email); if(!$response->execute()){ echo "Error query: " . $response->error . "."; } $result=$response->get_result(); while($res = $result->fetch_array()){ $name=$res['nameofuser']; //just an example } echo $name;
‘Tips’ add to real solution check if query is done.