English 中文(简体)
• 如何使PHP能够打印MySQL数据库的评论意见数目
原标题:How the get PHP to print the number of comments from MySQL database
  • 时间:2012-05-17 17:21:32
  •  标签:
  • php
  • mysql

我有以下问题。 我有三个表格:信息、评论和用户。 我想PHP从MySQL数据库中印刷以下领域的数据,作为我网页的表格:电文。 主题、用户名称和评论数目。 所有其他物品都做的是罚款,但我用斜线印制评论。

这是我迄今为止努力做到的:

<?php
 include("info.php");
 $connect;
 $sql="SELECT * FROM message, user
       WHERE message.userID = user.userID AND 
       ORDER BY message.messageID DESC";
 $result=mysql_query($sql) or die(mysql_error());

 $sql2="SELECT message.messageID, COUNT(*) as comments FROM comment 
        INNER JOIN
        message ON comment.messageID = message.messageID 
        GROUP BY comment.messageID";
 $result2=mysql_query($sql2) or die(mysql_error());

<table>
 <thead>
  <tr>
   <th>Subject</th>   
   <th>Sender</th>
   <th>Number of comments</th>
  </tr>
 </thead>
 <tbody>
  <?php
   while($rows=mysql_fetch_array($result)){
  ?>
  <tr>
   <td>
    <a href="php/message.php?id=<?php echo $rows[ messageID ]; ?>">
    <?php echo $rows[ subject ]; ?>
   </td>
   <td>
    <?php echo $rows[ username ]; ?>
   </td>                        
   <td>
    <?php while($rows2=mysql_fetch_array($result2)){ echo $rows2[ comments ];}?>
   </td>
  </tr>
 <?php
 } mysql_close(); ?>
 </tbody>
</table>
最佳回答

你们能否用一个问问点来看待所有这一切。

SELECT
    m.messageID,
    m.subject,
    u.username,
    c.numOfComments
FROM
    message m
    INNER JOIN user u ON m.userID = u.userID
    LEFT JOIN (SELECT COUNT(1) AS numOfComments, messageID FROM comments GROUP BY messageID) c ON m.messageID = c.messageID
ORDER BY
    m.messageID DESC

引文:

<?php
    include("info.php");
    connect();
    $sql = "
        SELECT
            m.messageID,
            m.subject,
            u.username,
            c.numOfComments
        FROM
            message m
            INNER JOIN user u ON m.userID = u.userID
            LEFT JOIN (SELECT COUNT(1) AS numOfComments, messageID FROM comments GROUP BY messageID) c ON m.messageID = c.messageID
        ORDER BY
            m.messageID DESC
    ";
    $result = mysql_query($sql) or die(mysql_error());

    echo "<table>
    <thead>
        <tr>
            <th>Subject</th>
            <th>Sender</th>
            <th>Number of comments</th>
        </tr>
    </thead>
    <tbody>
";
    while ($row = mysql_fetch_assoc($result))
    {
        $row = array_map("htmlspecialchars", $row); // sanitize to prevent XSS

        echo "      <tr>
            <td><a href="php/message.php?id={$row["messageID"]}">{$row["subject"]}</a></td>
            <td>{$row["username"]}</td>
            <td>{$row["numOfComments"]}</td>
        </tr>
";
    }
    echo "  </tbody>
</table>";
?>

There are a few other corrections as well. For instance, your link doesn t have a closing </a> tag, and your script may be vulnerable to XSS attacks. And then there s the nested while-loop, which was causing unnecessary complication and bugs.

问题回答

The problem i see is that you are nesting your while loops. Please see: http://www.php.net/manual/en/control-structures.while.php#52733

用一张纸张发言。

这是这样做的最佳方式(我认为):

$sql = "
SELECT message.*, user.*, commentsCounter.comments FROM message

INNER JOIN user
ON user.userID = message.userID

INNER JOIN ( SELECT COUNT( 1 ) as comments, comment.messageID FROM comment GROUP BY comment.messageID ) commentsCounter
ON commentsCounter.messageID = message.messageID

ORDER BY message.messageID DESC
";

为什么不像以下那样做?

$query = "SELECT comment FROM message where userId = $userId";

$result = mysql_query($query);

$numberOfComments = mysql_num_rows($result);

echo "$numberOfComments";

至少我相信,你们会重新寻找什么?

手册对此做了说明:http://php.net/manual/en/Function.mysql-num-rows.php rel=“nofollow”>mysql_num_rows(:

我sql_num_rows——因此获得多少行文

http://www.w3schools.com/sql/sql_func_count.asp

$result3=mysql_query("COUNT(*) FROM comment") or die(mysql_error());
$row=mysql_fetch_array($result3);
$numcomments = $row[0];
echo $numcomments;

我在几个地点使用(用不同的表格名称)。

Answer 2

假设你想对一个特殊职位发表评论,就象这样做:

$result3=mysql_query("SELECT DISTINCT message.messageID FROM comment ") or die(mysql_error());
$row=mysql_fetch_array($result3);
$numcomments = count($row);
echo $numcomments;




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签