Padnezz
Padnezz

Reputation: 89

MySQL Using AND condition dosen't output data

I have a simple WHERE clause that should grab some rows from a MySQL table.

In this WHERE clause I have a AND condition but it doesn't output anything...

        $query = " 
        SELECT 
           *
        FROM homework
        WHERE 
            for_type_id = :group_id
            AND for_type_id = :class_id   
        ";

        $query_params = array(
        ':group_id' => $_SESSION["user"]["group_id"],
        ':class_id' => $_SESSION["user"]["class_id"]
        );  

        try 
        { 
        $stmt = $db->prepare($query); 
        $stmt->execute($query_params); 
        } 
        catch(PDOException $ex) 
        { 
        die($ex->getMessage()); 
        } 

        $homework_data = $stmt->fetchAll();

The reason why I use "for_type_id" two times in the SELECT part, is because a user in the database have a "group_id" and a "class_id" for diffrent types of homework, so I want to grab all the homework for one user, using "class_id" and "group_id".

And again, it dosen't output anything. No errors, no notices, nothing.

Any advise, please?

Edit

Fixed typo

Upvotes: 0

Views: 24

Answers (1)

Robbert
Robbert

Reputation: 6592

You state the for_type_id is used to grab all homework for one user. If you use AND in your query, the returned rows must both be the group_id and class_id, which is almost certainly not possible. I think you may need to use an OR statement

$query = " 
    SELECT 
       *
    FROM homework
    WHERE 
        for_type_id = :group_id
        OR for_type_id = :class_id   
    ";

That should return everything that is either group_id or class_id

Upvotes: 3

Related Questions