2013-12-13 23 views
0

我寫了一個php文件,它想要打印出的電子郵件總數(總數),發送的電子郵件數量(發送)以及電子郵件數量還沒有發送給每個天。這裏是我的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>"; 
?> 

不幸的是,輸出只顯示錶和表的第一行。無法顯示電子郵件的數量。我能做些什麼來顯示這些數字。

+0

是什麼'$ sFlag'? –

+0

$ sflag是新變量的聲明,它保存sflag的選擇,這是從我的db – user3056561

+0

好吧,但是sFlag代表什麼?它的意思是什麼? –

回答

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>"; 

?> 
+0

感謝您幫助糾正我的SQL – user3056561

0

只是一些基本的調試改變這一行:

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

這樣:

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

沒有名爲$ list_array變量。

+0

它仍然無法解決我的問題。該程序仍然執行相同的輸出 – user3056561

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>"; 
?> 
+0

再次sql仍然有錯誤:#1064 - 你的SQL語法有錯誤;查看與您的MySQL服務器版本相對應的手冊,以便在''ecard2008'附近使用正確的語法WHERE'thedate'> ='2013-12-12'組'thedate'LIMIT 0,30'at line 1 – user3056561

+0

'thedate '> ='2013-12-12'表示日期可以在2013年12月12日或之後找到。也就是說,我想打印出2013-12-12 – user3056561

+0

之後的數據好吧...請檢查我的新答案與mysqli。它應該工作。否則...你必須檢查你的字段名稱並再次驗證 –