2016-10-03 57 views
0

這是我的代碼。問題是...我在db.leave類型和leavecount中有2個庫。目前我的代碼要離開類型並檢查離開類型對Emp ID和顯示總和..如果半天有2個計數agaist emp ID然後它顯示2 ..但我想先檢查leavetype,然後檢查離開計數,然後顯示結果。如何添加總和並在php中顯示錶mysql

<?php 
    $hostname="localhost"; 
    $username="root"; 
    $password=""; 
    $db = "usman"; 

    $dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password); 

    foreach($dbh->query("SELECT COUNT(*) as cnt 
     FROM ams 
     where empid= {$_SESSION['sess_user_id']} 
     GROUP BY leavetype 
     HAVING leavetype = 'HalfDay'") as $HalfDay) 
     { 
     echo "<table ><tr ><td style='border: 0px; '>" . $HalfDay['cnt'] . "</td></tr></table>";  
        echo "<br>"; 
     } 
?> 

回答

0

您可以檢查leavetypeempidHAVING之前不能把查詢的中間。

foreach($dbh->query("SELECT COUNT(*) as cnt 
    FROM ams 
    sum(leavetype) as leavecount 
    where leavetype = 'HalfDay' and 
    where empid= {$_SESSION['sess_user_id']} and 
    where leavecount > 2 
    GROUP BY leavetype 
    HAVING leavetype = 'HalfDay'") as $HalfDay) 

此代碼應該工作。

+0

..我的問題是,我想計數leavetype的半天,並從計數得到計數,然後顯示總和 –

+0

第一我希望sqli檢查假期類型,如果半天有2計數,然後去休假計算和總結半天計算 –

+0

你可以給你的數據表結構嗎? –