2016-08-24 59 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,并利用准备好的语句和变量绑定,你不需要担心引用。