PHP MySQL计数器/循环查询

时间:2011-02-18 12:33:13

标签: php mysql

我正在进行MySQL查询以搜索数据库中的项目。我已经从搜索表单中提取变量,但是我的WHERE子句遇到了一些问题。因为我不想搜索未在表单中输入的字段。我现在的代码是:

$query = "  SELECT RequestID, clients.ClientName, clients.Username, RequestAssignee, requests.StatusID, requests.PriorityID, StatusName, PriorityName
                                    FROM requests
                                    INNER JOIN clients ON requests.ClientID = clients.ClientID
                                    INNER JOIN statuses ON requests.StatusID = statuses.StatusID
                                    INNER JOIN priorities ON requests.PriorityID = priorities.PriorityID
                                    WHERE ";
                                    if(!empty($RequestID))
                                    {
                                        $query2 .= "RequestID = '" . $RequestID .  "' OR ";
                                    }
                                    if(!empty($ClientName))
                                    {
                                        $query2 .= "clients.ClientName = '" . $ClientName ."' OR ";
                                    }
                                    if(!empty($Username))
                                    {
                                        $query2 .= "clients.Username = '" . $Username . "' OR ";
                                    }
                                    if(!empty($RequestAssignee))
                                    {
                                        $query2 .= "RequestAssignee = '" . $RequestAssignee . "' OR ";
                                    }
                                    if(!empty($Status))
                                    {
                                        $query2 .= "statuses.StatusName = '" . $Status ."' OR ";
                                    }
                                    if(!empty($Priority))
                                    {
                                        $query2 .= "priorities.PriorityName = '" . $Priority ."'";
                                    }

但是,您可以看到一个问题,即如果有人只搜索一个字段,则查询会在结尾添加“OR”,从而导致错误:

SELECT RequestID, clients.ClientName, clients.Username, RequestAssignee, requests.StatusID, requests.PriorityID, StatusName, PriorityName FROM requests INNER JOIN clients ON requests.ClientID = clients.ClientID INNER JOIN statuses ON requests.StatusID = statuses.StatusID INNER JOIN priorities ON requests.PriorityID = priorities.PriorityID WHERE RequestID = '3' OR 

我猜我将不得不放置某种循环或计数器,但不确定如何接近它。有什么想法吗?

谢谢,马特。

2 个答案:

答案 0 :(得分:2)

$parts = array();
if(!empty($RequestID))
{
    $parts[] = "RequestID = '" . $RequestID .  "' ";
}
if(!empty($ClientName))
{
    $parts[] = "clients.ClientName = '" . $ClientName ."' ";
}
if(!empty($Username))
{
    $parts[] = "clients.Username = '" . $Username . "' ";
}
if(!empty($RequestAssignee))
{
    $parts[] = "RequestAssignee = '" . $RequestAssignee . "' ";
}
if(!empty($Status))
{
    $parts[] = "statuses.StatusName = '" . $Status ."' ";
}
if(!empty($Priority))
{
    $parts[] = "priorities.PriorityName = '" . $Priority ."' ";
}

$query2 .= implode(' OR ', $parts);

答案 1 :(得分:1)

好的,所以我接近这个的方法是构建将形成你的where子句separatley的变量:

这可以在数组中完成:

FIELD1=>'Value1';
FIELD2=>'Value2';

然后我会循环遍历这个数组,对于第一个元素,i = 1我将在WHERE中构建,对于i + n - > i +(n-1)我会预先挂起一个OR,对于最后一个数组值,我什么都不做。

然后我可以使用我在这个循环中构建的字符串 - 坚持我的查询字符串。

如果您需要更多帮助,请继续这样做,并给我们一个喊叫。这样做也稍微可维护。