douci4026 2010-07-14 21:17
浏览 26
已采纳

2个sql在一个php while循环中

I'm a bit of a novice to PHP and MySQL. I'm creating my own little news blog for the heck of it. I got to display the information I wanted, but what I want to do is count the number of comments. I made 2 tables, the first table is the article and the other is the comments. I not to sure of how to figure this out. Can anyone help?

$input_now1 = "SELECT blog_entries.id, blog_entries.posted, blog_entries.subject, blog_entries.quicktext
  FROM blog_entries
  ORDER BY blog_entries.posted DESC
  LIMIT 0, 3;";

$result1 = mysql_query($input_now1);

?>
<div id="middle">

  <div id="box1">
    <?php
      while($record1 = mysql_fetch_assoc($result1))
      {
        ?> 
        <h2 style="padding-top:5px;"><?php echo $record1['subject']; ?></h2>
        <div id="header1" style="position:relative;"></div>
        <div id="maintext" style="position:relative;">
          <p><?php echo $record1['quicktext']; ?></p>
          <a href="blog_articles.php?ID=<?php echo $record1["id"];?>">View Article - <?php echo date("D jS F Y g:iA",strtotime($record1["posted"]));?></a>
        </div>
        <div id="text_footer"></div>
        <?php 
      }
      mysql_free_result($result1);
    ?>
  </div>
  <?php
    include 'include/sidebar.php';
  ?>        

</div>

Table1 blog_entries: id posted subject body quicktext

Table2 blog_comments: id blog_id posted name comment

  • 写回答

3条回答 默认 最新

  • dpnfjx755573 2010-07-14 21:21
    关注

    We'd need to know your database schema to be sure, but it's probably something like this...

    SELECT COUNT(comment.id) AS numcomments,
        entry.id,entry.posted,entry.subject,entry.quicktext
    FROM `blog_comments` comment, `blog_entries` entry
    WHERE comment.blog_id=entry.id
    GROUP BY entry.posted, entry.subject, entry.quicktext
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失