开发者

PHP MySQL counter/loop query

开发者 https://www.devze.com 2023-02-12 02:18 出处:网络
I\'m doing a MySQL query to search for items in a database. I\'ve pulled variables out of the search form but I\'m having some problems with my WHERE clause. As I don\'t want to search on fields that

I'm doing a MySQL query to search for items in a database. I've pulled variables out of the search form but I'm having some problems with my WHERE clause. As I don't want to search on fields that haven't been input in the form. The code I have at the minute is:

$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 ";
                                    }
                          开发者_如何学C          if(!empty($RequestAssignee))
                                    {
                                        $query2 .= "RequestAssignee = '" . $RequestAssignee . "' OR ";
                                    }
                                    if(!empty($Status))
                                    {
                                        $query2 .= "statuses.StatusName = '" . $Status ."' OR ";
                                    }
                                    if(!empty($Priority))
                                    {
                                        $query2 .= "priorities.PriorityName = '" . $Priority ."'";
                                    }

However you can see an issue whereby if someone only searches one field, the query adds an 'OR' to the end, resulting in an error:

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 

Im guessing I'm going to have to put some sort of loop or counter in but unsure how to approach it. Any ideas?

Thanks, Matt.


$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);


Ok so the way i would approach this is to build the variables that will form your where clause separatley:

This could be done in an array:

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

I would then loop over this array, for the first element, i=1 i would build in a WHERE, for i+n -> i+(n-1) i would pre-pend an OR, for the last array value i wouldn't do anything.

I can then use the string i build in this loop - to stick into my query string.

Have a go at something like this and give us a shout if you need more help. Doing it this way is slightly more maintainable also.

0

精彩评论

暂无评论...
验证码 换一张
取 消