简体   繁体   中英

query returns nothing php

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.

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.

After execute query . fetch the results

   $stmt = $conn->prepare( "SELECT * FROM users WHERE email= ? ");

$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows === 0) exit('No rows');
while($row = $result->fetch_assoc()) {
// your code
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM