显示php表的数据和计算

时间:2013-12-13 00:51:44

标签: php mysql

我写了一个php文件,想要打印出电子邮件总数(总数),发送(发送)的电子邮件数量以及每天都没有发送的电子邮件数量。这是我的php文件如下

<?php      
    $total =0;
    $sent =0;
    $pending = 0;
    $sql = "SELECT `sflag` , `thedate` FROM `ecard2008` WHERE `thedate` >= '2013-12-12'";
    $list_mysql = mysql_query($sql) or mysql_error();
    while($list = mysql_fetch_array($list_array)) {
        $sflag = $list['sflag'];
        $date = $list['thedate'];
        if ($sflag == 0) {
            $pending = $pending + 1;
        }
        else {
            $sent = $sent + 1;
        }
        $total = $total + 1;
    }
    echo "<table border='1'>
    <tr>
        <th>Date</th>
        <th>Daily volume</th>
        <th>Sent</th>
        <th>Pending</th>
    </tr>";

    echo "<tr>\n";
    echo "<td>" . $date . "</td>";
    echo "<td>" . $total . "</td>";
    echo "<td>" . $sent . "</td>";
    echo "<td>" . $pending . "</td>";
    echo "</tr>";

    echo "</table>";
?>

不幸的是,输出只显示表和表的第一行。电子邮件的数量无法显示。我该怎么做才能显示这些数字。

3 个答案:

答案 0 :(得分:0)

只是一些基本的调试改变了这一行:

while($list = mysql_fetch_array($list_array)){

到此:

while($list = mysql_fetch_array($list_mysql)){

没有名为$ list_array的变量。

答案 1 :(得分:0)

<?php      
$sql = "SELECT 'thedate', count(sflag) as total, SUM(CASE WHEN sflag = '1' THEN 1 ELSE 0 END) 'sent', SUM(CASE WHEN sflag= '0' THEN 1 ELSE 0 END) 'pending' FROM 'ecard2008' WHERE 'thedate' >= '2013-12-12' group by 'thedate'";

$list_mysql = mysql_query($sql) or mysql_error();

echo "<table border='1'>
<tr>
    <th>Date</th>
    <th>Daily volume</th>
    <th>Sent</th>
    <th>Pending</th>
</tr>";


while($list = mysql_fetch_array($list_mysql)) {

echo "<tr>\n";
echo "<td>" . $list['thedate']. "</td>";
echo "<td>" . $list['total'] . "</td>";
echo "<td>" . $list['sent'] . "</td>";
echo "<td>" . $list['pending'] . "</td>";
echo "</tr>";

}

echo "</table>";
?>

答案 2 :(得分:0)

好的......你尝试使用mysqli,

<?php
// Create connection
$con=mysqli_connect("localhost","user","password","database");

// Check connection
if (mysqli_connect_errno($con))
  {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


$sql = "SELECT 'thedate', count(sflag) as total, SUM(CASE WHEN sflag = '1' THEN 1 ELSE 0 END) 'sent', SUM(CASE WHEN sflag= '0' THEN 1 ELSE 0 END) 'pending' FROM 'ecard2008' WHERE 'thedate' >= '2013-12-12' group by 'thedate'";

$list_mysql = mysqli_query($con, $sql);

echo "<table border='1'>
<tr>
<th>Date</th>
<th>Daily volume</th>
<th>Sent</th>
<th>Pending</th>
</tr>";


while($list = mysql_fetch_array($list_mysql)) {

echo "<tr>\n";
echo "<td>" . $list['thedate']. "</td>";
echo "<td>" . $list['total'] . "</td>";
echo "<td>" . $list['sent'] . "</td>";
echo "<td>" . $list['pending'] . "</td>";
echo "</tr>";

}

echo "</table>";

?>