Got To Figure
Got To Figure

Reputation: 422

SELECT * from table where?

I was working on this where I have 2 variables

And a table with few variables and 2 principal columns that I am going to use for this script

Now What I want it to do is to check if $pmid or $pmid is matching pmid in table or pmid2 any of the variables to match any of these two table columns mentioned Here's My script:

$selectpm = mysql_query("SELECT * FROM pm WHERE pmid=($chat_id OR $chat_id2) AND pmid2=($ chat_id OR $chat_id2)"); 

Of Course my code is wrong its just how I tough It could work , But I don't know how exactly is supposed to work thanks !

Upvotes: 1

Views: 486

Answers (3)

Pravat Kumar Sahoo
Pravat Kumar Sahoo

Reputation: 303

Please use:

 $selectpm = mysql_query("SELECT * FROM pm WHERE pmid
 IN($chat_id,$chat_id2) OR pmid2 IN($chat_id,$chat_id2)");

Upvotes: 3

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

This is how you can do it

SELECT * FROM pm 
WHERE 
( 
  pmid=$chat_id OR  pmid= $chat_id2
) 
AND (
 pmid2 = $chat_id OR pmid2 = $chat_id2
)


$selectpm = mysql_query(
"SELECT * FROM pm 
WHERE ( pmid=$chat_id OR  pmid= $chat_id2 )
 AND (pmid2 = $chat_id OR pmid2 = $chat_id2)"); 

A better way is to write the query in a variable as

$qry = "SELECT * FROM pm 
       WHERE 
       ( pmid=$chat_id OR  pmid= $chat_id2 )
       AND (pmid2 = $chat_id OR pmid2 = $chat_id2)" ;

$selectpm = mysql_query($qry);

Upvotes: 4

rjv
rjv

Reputation: 6766

You can use the in clause

SELECT * FROM pm 
WHERE pmid in ($chat_id , $chat_id2)
AND pmid2 in ($chat_id , $chat_id2)

Upvotes: 2

Related Questions