Nagini
Nagini

Reputation: 125

PHP SELECT query With condition

$query = mysql_query("SELECT M.msg_id, M.uid_fk, M.message, M.created, U.full_name, U.profile_pic, U.username, U.uid FROM t_haps_wall M, t_users U, t_join_user F WHERE
        M.uid_fk=U.uid AND F.uid=U.uid AND M.uid_fk = F.uid AND F.status='joining' order by M.msg_id desc ") or die(mysql_error());

That's my SELECT code to show some message and now I want to add 1 condition :

AND M.uid_fk='$uid'

So where I can put that in my complete code. I already tried but no effects. Or something missing / wrong code?
Please help. Thank you

Upvotes: 0

Views: 3075

Answers (4)

Abhilash
Abhilash

Reputation: 1610

You can add it wherever* you want after the WHERE clause. Try with this updated query

$sql =
"
SELECT 
   M.msg_id, 
   M.uid_fk, 
   M.message, 
   M.created, 
   U.full_name, 
   U.profile_pic, 
   U.username, 
   U.uid 
FROM 
   t_haps_wall M, 
   t_users U, 
   t_join_user F 
WHERE 
   M.uid_fk=U.uid 
   AND F.uid=U.uid 
   AND M.uid_fk = F.uid 
   AND F.status='joining' 
   AND M.uid_fk='".$uid."'
ORDER BY 
   M.msg_id DESC
";

$query = mysql_query($sql);

*I have added it at the end. Although, I say wherever and it works; it is not strictly the most optimal way. Placement of conditions can often influence how efficiently the queries run

Upvotes: 0

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79909

You should put it in the WHERE clause, and use the ANSI SQL-92 instead like so:

SELECT 
  M.msg_id, 
  M.uid_fk, 
  M.message, 
  M.created,
  U.full_name, 
  U.profile_pic, 
  U.username, 
  U.uid 
FROM t_haps_wall       M
INNER JOIN t_users     U ON  M.uid_fk = U.uid
INNER JOIN t_join_user F ON  F.uid    = U.uid
WHERE F.status = 'joining' 
  AND M.uid_fk ='$uid' -- your condition here 
order by M.msg_id desc 

Note that: Your code this way is vulnerable to SQL injection , use PDO or prepared statements instead. See this for more details:

Upvotes: 1

Sashi Kant
Sashi Kant

Reputation: 13465

It would have been better if you have searched first before raising this question ::

Anyways :: check the placement of NEW_COLUMN

$query = mysql_query("SELECT M.msg_id, M.uid_fk, M.message, M.created, U.full_name, U.profile_pic, U.username, U.uid, NEW_COLUMN FROM t_haps_wall M, t_users U, t_join_user F WHERE
        M.uid_fk=U.uid AND F.uid=U.uid AND M.uid_fk = F.uid AND F.status='joining' order by M.msg_id desc ") or die(mysql_error());

Upvotes: 0

George
George

Reputation: 36784

Like this?..

$query = mysql_query("SELECT M.msg_id, M.uid_fk, M.message, M.created, U.full_name, U.profile_pic, U.username, U.uid FROM t_haps_wall M, t_users U, t_join_user F WHERE
    M.uid_fk=U.uid AND F.uid=U.uid AND M.uid_fk = F.uid AND F.status='joining' AND M.uid_fk=".$uid." order by M.msg_id desc ") or die(mysql_error());

I was assuming you wanted to place the M.uid_fk=".$uid." in your WHERE condition. The (.) operator isn't really necessary because you have double quotes around your query, but I like to use it anyway.

Upvotes: 0

Related Questions