我写了一个php文件,想要打印出每天的电子邮件总数(total)、已发送(sent)的电子邮件数量和未发送的电子邮件数量。这是我的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>";
?>
不幸的是,输出仅显示表格和表格的第一行。无法显示电子邮件数量。我该怎么做才能显示这些数字。
最佳答案
好吧...你尝试使用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>";
?>
关于php - 显示php表的数据和计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20557227/