使查询显示最新更新

时间:2013-10-13 23:26:48

标签: php sql

所以我有一个脚本显示所有发布的主题与主题相关的信息,我想知道是否有人可以帮助我做到这一点,只要有人评论或如果帖子被编辑,它会碰到它查询的第一行是什么?

    function getReplies($id){
        $q = @mysql_query("SELECT reply_content, reply_date, reply_by FROM replies WHERE reply_id='$id'");
        if(!$q){
            echo 'Error: '.mysql_error();
        }

        $res = mysql_fetch_array($q);
        $q2 = @mysql_query("SELECT `topic_subject` FROM `topics` WHERE `topic_id`='$id'");
        if(!$q2){
            echo 'Error: '.mysql_error();
        }

        $res2 = mysql_fetch_array($q2);
        if($_SESSION['id'] == $res['reply_by']){
            echo '<div class="panel panel-default">
  <div class="panel-heading"><b>'.$res2['topic_subject'].'</b></div>
  <div class="panel-body">
   '.nl2br($res['reply_content']).'
  </div>
                <div class="panel-footer">
                    Posted By <strong><a href="../public.php?id='.$res["reply_by"].'">'.getOwner($res['reply_by']).'</stong></a>
                    on <strong>'.$res['reply_date'].'</strong><br />
                    <a href="edit.php?id='.$id.'">Edit</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a href="delete.php?id='.$id.'">Delete</a>
                </div>
</div>
            </div>';
        } else {
            echo'<div class="panel panel-default">
  <div class="panel-heading"><b>'.$res2['topic_subject'].'</b></div>
  <div class="panel-body">
   '.nl2br($res['reply_content']).'
  </div>
                <div class="panel-footer">
                    Posted By <strong><a href="../public.php?id='.$res["reply_by"].'">'.getOwner($res['reply_by']).'</stong></a>
                    on <strong>'.$res['reply_date'].'</strong><br />
                </div>
</div>
            </div>';
        }

    }

评论: Comments structure in DB

文章: Posts structure in DB

主题: Topics structure in DB

1 个答案:

答案 0 :(得分:1)

尝试更改您的第一个查询并将其授予ORDER BY,或许?

$q = "SELECT   reply_content, reply_date, reply_by
      FROM     replies
      WHERE    reply_id='$id'
      ORDER BY reply_date DESC";

在我看来,你正在使用两个查询,你只需要使用一个查询。

更新:优化代码,尽量不重复自己,并加入查询

function getReplies($id) {

      $query = "SELECT   replies.reply_content,
                         replies.reply_date,
                         replies.reply_by
                         topics.topic_subject
                FROM     replies,
                         topics
                WHERE    replies.reply_id = topics.topic_id
                  AND    replies.reply_id = '$id' " . //<== PS: You should sanitize this $id parameter
              " ORDER BY replies.reply_date DESC";
                // You can replace the replies.reply_date column by whatever you end up with for sorting

      // Important note - Look into mysqli, mysql is deprecated and too old to be used these days.
      $q = @mysql_query($query);

      if(!$q) {
          echo 'Error: '.mysql_error();
      }

      $res = mysql_fetch_array($q);

      $output = '
        <div class="panel panel-default">
          <div class="panel-heading"><b>'.$res['topic_subject'].'</b></div>
            <div class="panel-body">
              '.nl2br($res['reply_content']).'
            </div>
            <div class="panel-footer">
                Posted By <strong><a href="../public.php?id='.$res["reply_by"].'">'.getOwner($res['reply_by']).'</stong></a>
                on <strong>'.$res['reply_date'].'</strong><br />';

      if($_SESSION['id'] == $res['reply_by'])
        $output .= '<a href="edit.php?id='.$id.'">Edit</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a href="delete.php?id='.$id.'">Delete</a>';

      $output .= '
            </div>
          </div>
        </div>';

      echo $output;

    }