使用PHP& amp;字段搜索的组合MYSQL

时间:2014-02-11 02:06:37

标签: php mysql search

我正在使用PHP& amp; MYSQL。

其中一项任务是搜索任何字段组合。这包括从数据库填充的下拉框。和文本字段。

t2ath包含

ID
SPORT
COUNTRY
GENDER
FIRSTNAME
LASTNAME
Image

我已经在这个代码上工作了一个星期,可以搜索任何没有错误的组合。

我想知道是否还有另一种更有效的方法。

$selectedSport = $_POST['sport']; $gender =$_POST['gender']; $fName =$_POST['fname']; $lName =$_POST['lname']; $country =$_POST['country'];
$sql_fName=""; $sql_lName=""; $sql_gender=""; $sql_sport=""; $sql_country="";
$checkFiled=False;
$where="";
$and="";
//
if ( $selectedSport=="showAll")
    {
        !isset($selectedSport);
    }
else
    {
        if (isset($selectedSport)) 
            { 
                if ($checkFiled==True)
                    {

                        $sql_sport = " AND t2ath.sport = '$selectedSport'" ; 

                    }
                else
                    {
                        $sql_sport = " t2ath.sport = '$selectedSport' " ; 
                        $checkFiled=True;   
                    } 
            }
        else {
            $sql_sport = "";  
        }
    }

//
if ( $country =="showAll")
    {
        !isset($country);
    }
else
    {
        if (isset($country)) 
            { 
                if ($checkFiled ==True)
                    {

                        $sql_country = " AND t2ath.country = '$country'" ; 

                    }
                else
                    {
                        $sql_country = " t2ath.country = '$country' " ; 
                        $checkFiled=True;
                    } 
            }
        else {
            $sql_country = "";  
        }
    }
//
if ( $gender=="Gender")
    {
        !isset($gender);
    }
else
    {
        if (isset($gender)) 
            { 
                if ($checkFiled ==True)
                    {

                        $sql_gender = " AND t2ath.gender = '$gender'" ; 

                    }
                else
                    {
                        $sql_gender = " t2ath.gender = '$gender' " ; 
                        $checkFiled=True;
                    } 
            }
        else {
            $sql_gender = "";  
        }
    }
//
if ($fName =="")
    {
        !isset($fName);
    }
else
    {
        if (isset($fName)) 
            { 
                if ($checkFiled==True)
                    {

                        $sql_fName = " AND t2ath.firstName = '$fName'" ; 
                    }
                else
                    {
                        $sql_fName = " t2ath.firstName = '$fName' " ; 
                        $checkFiled=True;   
                    } 
            }
        else {
            $sql_fName = "";  
        }
    }
//
if ($lName =="")
    {
        !isset($lName);
    }
else
    {
        if (isset($lName)) 
            { 
                if ($checkFiled==True)
                    {

                        $sql_lName = " AND t2ath.lastName = '$lName' " ; 

                    } 
                else
                    {
                        $sql_lName = " t2ath.lastName = '$lName' " ; 
                        $checkFiled=True;
                    }
            }
        else
            {
                $sql_lName = "";  
            }
    }

if ($checkFiled == True)
    $where=" where ";

$selectString = "SELECT t2ath.lastName,t2ath.firstName,t2ath.image,t2ath.sport,t2ath.gender,t2ath.country,t2country.flag FROM t2ath LEFT JOIN t2country
                 ON t2ath.country = t2country.name $where  $sql_sport   $sql_country $sql_gender $sql_fName $sql_lName  ";
$result = mysql_query($selectString);

3 个答案:

答案 0 :(得分:1)

在连接查询时,不是关于是否添加AND的所有条件,而是使用数组和implode

$fields = array('sport' => 'sport',
                'gender' => 'gender', 
                'fname' => 'firstName',
                'lname' => 'lastName',
                'country' => 'country');
$wheres = array();
foreach ($fields as $postfield => $dbfield) {
    if ($_POST[$postfield] != 'showAll') {
        $wheres[] = "$dbfield = '" . mysql_real_escape_string($_POST[$postfield]) . "'";
    }
}
$selectString = "SELECT t2ath.lastName, t2ath.firstName, t2ath.image, t2ath.sport, t2ath.gender, t2ath.country, t2country.flag 
                 FROM t2ath LEFT JOIN t2country
                 ON t2ath.country = t2country.name";
if (count($wheres) > 0) {
    $selectString .= " WHERE " . implode(" AND ", $wheres);
}
$result = mysql_query($selectString);

要了解如何使用PDO预处理语句进行类似操作,请参阅我的答案:What code approach would let users apply three optional variables in PHP/MySQL?

答案 1 :(得分:0)

为什么不用每一列来评估你的字符串(这只是一个指南,我不是在那里构建你的PHP代码:

SELECT 
  * 
FROM 
  table
WHERE 
  (ID = $id OR $id = 'showAll')
  AND (SPORT = $sport OR $sport = 'showAll')
  AND (COUNTRY = $country OR $country = 'showAll')
  AND (GENDER = $gender OR $gender = 'showAll')
  AND (FIRSTNAME = $firstname OR $firstname = 'showAll')

只需要确保将变量NVL设置为适当的值(无论是int还是字符串)

答案 2 :(得分:0)

我过去做过类似的事情,我检查了不同字段的值,然后将它们添加到一系列数组中。我为select,from,where,order创建了一个数组。您可以对其他集合(如组或限制)执行类似操作。然后我运行'array_unique',将它们内爆并将它们放入SQL字符串中。

$array_select = array('users.Id'); // SET SOME DEFAULTS SO THE QUERY WILL ALWAYS RUN
$array_from = array('users');
$array_where = array();
$array_order = array();

if (isset($first_name)) {
    $array_select[] = 'First_Name';
    $array_from[] = 'users';
}

if (isset($city)) {
    $array_select[] = 'City';
    $array_from[] = 'user_contact';
    $array_where[] = 'users.Id = user_contact.City';
}

if ($array_select) {
    $array_select = array_unique($array_select);
    $string_select = implode(', ', $array_select);
}
if ($array_where) {
    $array_where = array_unique($array_where);
    $string_where = 'WHERE '.implode(' AND ', $array_where);
}
// REPEAT FOR OTHERS ...



// BUILD THE QUERY OUT
$sql = 'SELECT '.$string_select.' FROM '.$string_from.' '.$string_where.' ...