Volcan 3
Volcan 3

Reputation: 109

use php to select * from table where 2 entries have to match

what i want is to be able to select all from a table where bout x = x and y = y, if x = y and y = y i dont want it count. pritty much 2 entries where bouth have to match. I know about SELECT * FROM 'messages' WHERE read = '0', id = $id; but doesnt that select bouth rows where id = $id and read = '0' ?

The whole point of this is to display x messages that the specific user have recived but not yet opened, might be a better way to do it but this is what ive come to so far:

<?php
    $id = htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8');
    $db = mysqli_connect("localhost", "root", "password", "database") or die ("Could not connect to database");

    $inboxquery = mysqli_query ($db, "SELECT * FROM message WHERE to_id='$id'");
        while($row = mysqli_fetch_array($inboxquery, MYSQLI_ASSOC)){
            $to_id = $row['to_id'];
            $read = $row['read'];
        }


    $x = mysqli_num_rows($result)

    if($x == >0){
        echo"<span>".$value." <a class='a' href='/Inbox'> Inbox</span>";
    }else{
        echo"<span><i class='fa fa-inbox'></i></span> <a class='a' href='/Inbox'> Inbox";
    }
?>

Upvotes: 0

Views: 56

Answers (2)

Saskia
Saskia

Reputation: 524

Not sure if I understand your question correctly, but if you want to show rows that have read = 0 or id = id (so show both rows that have either one of them), simply use OR:

SELECT * FROM message WHERE to_id = '$id' OR read = 0

Upvotes: 0

Duane Lortie
Duane Lortie

Reputation: 1260

Not sure I fully understand the question but assuming an unread message has a zero in the read column, this should work..

$inboxquery = mysqli_query ($db, "SELECT * FROM message WHERE read = 0 AND to_id='$id'");

Upvotes: 1

Related Questions