将数组值添加到where子句

时间:2012-11-02 16:46:32

标签: php mysql

使用此代码..我需要将$ code变量添加到sql查询的where子句中。

if ( isset( $_POST['code_search_submitted'])) {

        if ( isset( $_POST['searchCode']) && !empty( $_POST['searchText'])) {           


    $searchCode = $_POST['searchCode']; 
        $searchText = $_POST['searchText'];     

        echo $searchCode;
        echo '<br />';
        echo $searchText;
        //if (is_numeric( $searchText ) && (strlen($searchText) == 4)) {    

        $numbers = explode(",", $searchText);
        foreach($numbers as $number) {
            $numbers =  trim($number);


            if (ctype_digit($numbers) && (strlen($numbers) == 4)) { 

                $code = $numbers ;

            } else {

                echo 'Can not cantain string and more than 4 digists number';
            }

        }
}
}

这是我的查询...

SELECT 
    tcs.tutor_id AS tid, 
    t.qualification,
    GROUP_CONCAT( DISTINCT o.option_name SEPARATOR ', ') AS tutor_option, 
    timg.image_name AS img, 
    city_name AS city,
    d.district_name AS district
FROM tutor_category_subject as tcs
    INNER JOIN subject AS s ON tcs.subject_id = s.subject_id
    INNER JOIN tutors AS t ON tcs.tutor_id = t.tutor_id
    INNER JOIN address ON address.address_id = t.address_id
    INNER JOIN city ON city.city_id = address.city_id
    INNER JOIN district AS d ON d.district_id = city.district_id
    LEFT JOIN tutor_images AS timg ON timg.tutor_id = tcs.tutor_id AND timg.image_type = 'profile'
WHERE t.tutor_code = ------------
GROUP BY tcs.tutor_id;

$ code变量有更多的成员代码,如2434,2345,5345,2354等等......有人可以告诉我如何在where子句中使用它?

谢谢...

3 个答案:

答案 0 :(得分:0)

if ( isset( $_POST['code_search_submitted'])) {

        if ( isset( $_POST['searchCode']) && !empty( $_POST['searchText'])) {           


    $searchCode = $_POST['searchCode']; 
        $searchText = $_POST['searchText'];     

        echo $searchCode;
        echo '<br />';
        echo $searchText;
        //if (is_numeric( $searchText ) && (strlen($searchText) == 4)) {    

        $numbers = explode(",", $searchText);

        $countmax=count($numbers)-1;

        foreach($numbers as $key=>$number) {
            $numbers =  trim($number);


            if (ctype_digit($numbers) && (strlen($numbers) == 4)) { 
                if($key==$countmax)
                {
                $code .= "t.tutor_code='".$number."'" ;
                }
                else
                {
                $code .= "t.tutor_code='".$number."' || " ;
                }

            } else {

                echo 'Can not cantain string and more than 4 digists number';
            }

        }
}
}


/////////////////////////////////////
//Query Updated
/////////////////////////////////


SELECT 
    tcs.tutor_id AS tid, 
    t.qualification,
    GROUP_CONCAT( DISTINCT o.option_name SEPARATOR ', ') AS tutor_option, 
    timg.image_name AS img, 
    city_name AS city,
    d.district_name AS district
FROM tutor_category_subject as tcs
    INNER JOIN subject AS s ON tcs.subject_id = s.subject_id
    INNER JOIN tutors AS t ON tcs.tutor_id = t.tutor_id
    INNER JOIN address ON address.address_id = t.address_id
    INNER JOIN city ON city.city_id = address.city_id
    INNER JOIN district AS d ON d.district_id = city.district_id
    LEFT JOIN tutor_images AS timg ON timg.tutor_id = tcs.tutor_id AND timg.image_type = 'profile'
WHERE 1=1 && ( $code ) 
GROUP BY tcs.tutor_id;

答案 1 :(得分:0)

Updated code for query:

$query="SELECT 
    tcs.tutor_id AS tid, 
    t.qualification,
    GROUP_CONCAT( DISTINCT o.option_name SEPARATOR ', ') AS tutor_option, 
    timg.image_name AS img, 
    city_name AS city,
    d.district_name AS district
FROM tutor_category_subject as tcs
    INNER JOIN subject AS s ON tcs.subject_id = s.subject_id
    INNER JOIN tutors AS t ON tcs.tutor_id = t.tutor_id
    INNER JOIN address ON address.address_id = t.address_id
    INNER JOIN city ON city.city_id = address.city_id
    INNER JOIN district AS d ON d.district_id = city.district_id
    LEFT JOIN tutor_images AS timg ON timg.tutor_id = tcs.tutor_id AND timg.image_type = 'profile'
WHERE 1=1";
if($code!="")
{
$query.=" && ( $code ) ";
}
$query.="GROUP BY tcs.tutor_id";

答案 2 :(得分:0)

您可以使用array_map转换/处理每个值,然后将它们组合回来并使用in子句:

$numbers = explode(",", $searchText);
$parsed = array_map(function ($a) {
   if (ctype_digit($a) && (strlen($a) == 4)) { 
        return "'$a'" ;
    } else {
        echo 'Can not cantain string and more than 4 digists number';
    }
},
$numbers);

$numbers = '(' . preg_replace('/\,+/', ',',implode(',', $parsed)) . ')';

然后

SELECT 
    tcs.tutor_id AS tid, 
    t.qualification,
    GROUP_CONCAT( DISTINCT o.option_name SEPARATOR ', ') AS tutor_option, 
    timg.image_name AS img, 
    city_name AS city,
    d.district_name AS district
FROM tutor_category_subject as tcs
    INNER JOIN subject AS s ON tcs.subject_id = s.subject_id
    INNER JOIN tutors AS t ON tcs.tutor_id = t.tutor_id
    INNER JOIN address ON address.address_id = t.address_id
    INNER JOIN city ON city.city_id = address.city_id
    INNER JOIN district AS d ON d.district_id = city.district_id
    LEFT JOIN tutor_images AS timg ON timg.tutor_id = tcs.tutor_id AND timg.image_type = 'profile'
WHERE t.tutor_code IN $numbers
GROUP BY tcs.tutor_id;