2016-06-17 52 views
0

我有一個PHP最後一個動作列表,它從MySQL獲取結果並顯示數據,但我只需要通過電話調用來定義它。 MYSQL的字段是 「類型」 和電話通話是 「P」PHP MySQL結果僅通過電話過濾

例過濾日期> $型= P

<?php 
$db_host = "hidden"; 
$db_user = "hidden"; 
$database= "hidden"; 
$db_pwd= "hidden"; 
$conn = mysql_connect($db_host, $db_user, $db_pwd) or die(mysql_error()); 
mysql_select_db($database, $conn) or die(mysql_error()); 
$epoch = date('U'); 
$dayEnd = strtotime("midnight", $epoch); 
$dayStart = strtotime("tomorrow", $dayEnd) - 1; 
$query = mysql_query("select FROM_UNIXTIME(a.maxepoch,\"%d-%m-%Y\") , b.id, b.sid, b.did, b.type, b.nextaction, FROM_UNIXTIME(b.nextactiondate,\"%d-%m-%Y\") from History b, 
(select max(epoch) as maxepoch from History 
group by did) a 
#where a.maxepoch = b.epoch and b.epoch < (unix_timestamp(now()) - 2592000) 
where a.maxepoch = b.epoch and b.epoch < (unix_timestamp(now()) - 2419200) 
order by b.epoch desc"); 
$mydate = date("d-m-Y"); 
$message = "<br><br><h1>30 day Report for $mydate</h1><br>"; 
$message .= "<table border=\"1\"><tr><strong><td>Client Ref</td><td>Customer Ref</td><td>Action</td><td>Action Date</td><td>Next Action</td><td>Next Action Date</td></strong></tr>"; 
while ($def = mysql_fetch_row($query)) { 
    $sid = $def[2]; 
    $did = $def[3]; 
    $type = $def[4]; 
    $nextaction = $def[5]; 
    $nextactiondate = $def[6]; 
    $maxepoch = $def[0]; 
$message .= "<tr><td>$sid</td><td>$did</td><td>$type</td><td>$maxepoch</td><td>$nextaction</td><td>$nextactiondate</td></tr>"; 
    } 
    mysql_free_result($query); 
    mysql_close($conn); 
    $message .= "</table>"; 
    $message .= "</body></html>"; 
echo $message; 
?> 

回答

0

只要把簡單的條件到您的while週期:

while ($def = mysql_fetch_row($query)) { 
    if ($def[4] === 'P') { //<!-- here is the condition 
     $sid = $def[2]; 
     //... more varaibles 
     $message .= "<tr><td>$sid</td><td>$did</td><td>$type</td><td>$maxepoch</td><td>$nextaction</td><td>$nextactiondate</td></tr>"; 
    } 
} 
+0

從性能角度來看,我t在MySQL查詢中執行此操作要好得多,因爲@Vidalia建議:https://stackoverflow.com/a/37880398/2770263 – Kristian

+0

是的,它是我知道的。只是OP想要稍後重用整個結果集,或者它的一部分。 – vaso123

1

你可以在MySQL中用WHERE子句很容易做到這一點。

select FROM_UNIXTIME(a.maxepoch,\"%d-%m-%Y\") , b.id, b.sid, b.did, b.type, b.nextaction, FROM_UNIXTIME(b.nextactiondate,\"%d-%m-%Y\") 
from History b, 
    (select max(epoch) as maxepoch from History group by did) a 
    where a.maxepoch = b.epoch and b.epoch < (unix_timestamp(now()) - 2419200) 
where b.type = 'P' 

order by b.epoch desc