0

我对这段代码有语法问题:

$query = "SELECT *
        FROM ".$db->nameQuote('#__mls')."
        WHERE 1=1"
            . if ($zip != null)
                { AND ".$db->nameQuote('MSTZIP')." = ".$db->quote($zip)."}
            . if ($city != null)
                { AND ".$db->nameQuote('MSTCITY')." = '".$db->quote($city)."'}
            . if ($bdrms != null)
                { AND ".$db->nameQuote('MSTBDRMS')." >= ".$db->quote($bdrms)."}
            . if ($bths != null)
                { AND ".$db->nameQuote('MSTBATHS')." >= ".$db->quote($bths)."}
            . if ($lprice != null)
                { AND ".$db->nameQuote('MSTLISTPRC')." BETWEEN ".$db->quote($lprice)." AND ".$db->quote($hprice)."} 
        ";"
    ;

第一个字符串"开始查询语句,第二"层分配表,当WHERE语句时,它变得棘手。所有的if陈述都在惹我生气。我觉得这就是 a"被放错地方或丢失的地方。

4

2 回答 2

1

AND您错过每个人之前",您需要以不同的方式构建您的查询(请仔细检查每个'我确定我错过了一些地方)

   $query = "SELECT *
    FROM ".$db->nameQuote('#__mls')."
     WHERE 1=1";
         if ($zip != null)
             { $query .= " AND ".$db->nameQuote('MSTZIP')." = '".$db->quote($zip)."'";}
         if ($city != null)
              {$query .= " AND ".$db->nameQuote('MSTCITY')." = '".$db->quote($city)."'";}

          if ($bdrms != null)
             {$query .= " AND ".$db->nameQuote('MSTBDRMS')." >= '".$db->quote($bdrms)."'";}
        if ($bths != null)
            {$query .= " AND ".$db->nameQuote('MSTBATHS')." >= '".$db->quote($bths)."'";}
         if ($lprice != null)
             {$query .= " AND ".$db->nameQuote('MSTLISTPRC')." BETWEEN '".$db->quote($lprice)." AND ".$db->quote($hprice).";} 

;
于 2012-12-06T15:04:39.560 回答
1

彻底破碎,难以置信。尝试更多类似的东西:

$query = "SELECT * FROM " . $db->nameQuote('#__mls') . " WHERE 1=1";
$clauses = array();
if ($zip != null) {
   $clauses[] = $db->nameQuote('MSTZIP') . " = " . $db->quote($zip);
}
if (etc...) {
   ... 
}
$query .= implode(' AND ', $clauses);
echo $query;
于 2012-12-06T15:05:25.857 回答