2016-08-24 33 views
-1

我需要過濾這些結果與c.statusRP =狀態打開如何將條件添加到WHERE子句中?

$query= mysql_query("SELECT h.id, h.sid, h.did, h.nextaction, h.nextactiondate, d.company, d.name, d.surname, c.company, d.balance, d.amount from History h, Person d, Client c where h.sid=c.ref and d.id=h.did and nextactiondate between $dayEnd and $dayStart order by c.company desc"); 

    #print $tquery; 
    $mydate = date("d-m-Y"); 

    $message = "<br><br><h1>Task List for $mydate</h1><br>"; 

    $message .= "<table border=\"1\"><tr><strong><td>Customer Ref</td><td>Client Company</td><td>Customer Ref</td><td>Other Company</td><td>Full Name</td><td>Next Action</td></strong></tr>"; 

    while ($def = mysql_fetch_row($query)) { 

    $sid = $def[1]; 
    $did = $def[2]; 
    $nextaction = $def[3]; 
    $nextactiondate = $def[4]; 
    $dcompany = $def[5]; 
    $dname = $def[6]; 
    $dsurname = $def[7]; 
    $ccompany = $def[8]; 
    $dbalance = $def[9]; 
    $damount = $def[10]; 

    if ($dbalance == "") { 
     $newbalance = $damount; 
    } 
    else { 
     $newbalance = $dbalance; 
    } 

    switch ($nextaction) { 

我曾嘗試加入

$query= mysql_query("SELECT h.id, h.sid, h.did, h.nextaction, 
          h.nextactiondate, d.company, d.name, 
          d.surname, c.company, d.balance, 
          d.amount 
        from History h, Person d, Client c 
        where h.sid=c.ref 
         and d.id=h.did 
         and nextactiondate between $dayEnd and $dayStart 
        order by c.company desc 
        WHERE c.statusRP = 'Open';"); 

但隨後我上線的錯誤顯示這樣的:

while ($def = mysql_fetch_row($query)) { 

我在做什麼錯了?

+0

如果仔細觀察,您會看到原始查詢中有ALREADY WHERE子句。也許你應該尋找另一種熱情。或者閱讀一個SQL手冊,僅僅爲了它的樂趣 – RiggsFolly

+0

格式化你的查詢,所以你可以真正理解它們是什麼 – RiggsFolly

+0

錯誤是因爲查詢失敗 – RiggsFolly

回答

2

您已有WHERE,您需要將其加入AND,並將其放在ORDER BY之前。

$query= mysql_query("SELECT h.id, h.sid, h.did, h.nextaction, h.nextactiondate, d.company, d.name, d.surname, c.company, d.balance, d.amount 
    from History h, Person d, Client c 
    where h.sid=c.ref 
     and d.id=h.did 
     and nextactiondate between $dayEnd and $dayStart 
     AND c.statusRP = 'Open' 
     order by c.company desc;"); 

其他注意事項:$dayEnd$dayStart也許應該被引用,除非他們時間戳。

您已經開放了SQL注入。 mysql_ *函數已被棄用,並從PHP7中完全刪除。切換到PDO或mysqli,並利用準備好的語句和變量綁定,你不需要擔心引用。

相關問題