0

アクティビティ フィード システムを作成したいのですが、フィード (ステータス) と友達がデータベースの別のテーブルにあります。ログインしたユーザーが友達からのフィードのみを受信できるようにするにはどうすればよいですか。

<?php
$sql = "
SELECT * FROM status WHERE author='(friend of logged-in user)' AND type='a'
**UNION** 
SELECT * FROM friends WHERE user1='$user' AND accepted='1' OR user2='$user' AND accepted='1' 
";
$query = mysqli_query($database, $sql);
$statusnumrows = mysqli_num_rows($query);
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
    $user1 = $row["user1"];
    $user2 = $row["user2"];
    $accepted = $row["accepted"];
    $statusid = $row["id"];
    $account_name = $row["account_name"];
    $author = $row["author"];
    $postdate = $row["postdate"];
    $postdate = strftime("%b %d, %Y %I:%M %p");
    $data = $row["data"];
    $data = nl2br($data);
    $data = str_replace("&amp;","&",$data);
    $data = stripslashes($data);
    $statusDeleteButton = '';
    if($author == $log_username || $account_name == $log_username ){
        $statusDeleteButton = '<span id="sdb_'.$statusid.'"><a href="#" onclick="return false;" onmousedown="deleteStatus(\''.$statusid.'\',\'status_'.$statusid.'\');" title="DELETE THIS STATUS AND ITS REPLIES">delete status</a></span> &nbsp; &nbsp;';
    }

    $feedlist .= '<div id="status_'.$statusid.'" class="flipwrapper pin">
        <div class="picture">
        <header class="img-btm">
            '.$postdate.'</b><br />
            20 <a href="#">cmts</a>&nbsp;255 <a href="#">likes</a>&nbsp; '.$statusDeleteButton.'
        </header>
        <a href="status_frame.php?id='.$statusid.'"><img id="bound" src="'.$data.'"/></a></div></div>';
}
?>
4

1 に答える 1

1

ここではUNIONは絶対に必要ありませんが、多かれ少なかれ次のようなサブクエリが必要です。

SELECT * FROM status 
WHERE author in (
        SELECT whateverfieldshouldmapfromfriendstoauthor FROM friends 
        WHERE user1='$user' AND accepted='1' OR user2='$user' AND  accepted='1' 
    ) AND type='a'

そして、いくつかの一般的なヒント:

  • スクリプト言語 (この場合は php) で直接クエリを作成することは、非常に悪い考えです。クエリを作成して実行し、結果セットを検査できるツールを使用します。MySQL 独自のWorkbenchSquirrel SQLなど、たくさんあります。
  • SQL インジェクションの問題には十分注意してください ($user変数がリクエストによって提供されている場合は、そうです)。SQL インジェクションの問題を回避する最善の方法は、パラメータ化されたクエリを使用することです。
于 2013-05-04T00:19:25.847 に答える