skip to Main Content

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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search