开发者

Multiple Conditional Operators mysql_query

How would I construct a query with the following base function

$query = mysql_query("SELECT * FROM frien开发者_JAVA百科d_requests WHERE confirmed=1 AND (sender_id='".$user_id."' OR recipient_id='".$user_id."')");

Where $user_id is the current ID held by the session.


Make sure to call session_start() to initiate session handling on the page. And make sure to use mysql_real_escape_string() to protect against SQL injection. Your question is a little unclear in terms of what exactly you want from the session. If your $user_id variable is a value that is stored in the session, you can retrieve it from the $_SESSION superglobal. If $user_id is the actual session id, you can retrieve it using the session_id() function.

<?php
session_start();
// ...
$user_id = $_SESSION['user_id']; // or $user_id = session_id();

$query = mysql_query("SELECT * FROM friend_requests WHERE confirmed=1 AND "
    . "(sender_id='" . mysql_real_escape_string($user_id) . "' OR "
    . "recipient_id='" . mysql_real_escape_string($user_id) . "')");
// ...
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜