开发者

PDO in PHP : problem in binding parameter

I want to pass (1,2,3) this to an IN query of where clause. something like this.

$sql = $db->prepare("
    SELECT 
        id, attribution,..........................
    FROM
        filterdaiict
    WHERE Checkbox
        IN (:checkbox)
    HAVING
        distance < :radius
    ORDER BY
        distance ASC
    LIMIT
        0, 50 
");

$sql->bindParam(
    ':checkbox',
    GetCheckboxValue ( $value['CHECKBOXLIST'] ),
    PDO::PARAM_STR 
);

where t开发者_Python百科he function GetCheckboxValue ( $value['CHECKBOXLIST'] ) returns string like 1,2,3. This code does not work. I do not know where the errors could be. Let me know. Thanks in advance.


This example builds on SamT's answer above to build a query string but still bind the parameters.

// Get your IDs into an array
$ids = explode(',', GetCheckboxValue($value['CHECKBOXLIST']));

// Build a list of placeholders that we can use in the query
$params = array();
foreach ($ids as $idx => $val) {
     $key = ':checkbox' . $idx;
     $params[$key] = $val;
}    

// Join the keys to use as placeholders
$querystr = implode(', ', array_keys($params));

// Prepare our statement using the placeholders we made above
$sql = $db->prepare( " SELECT id, attribution,...... .................... 
    FROM filterdaiict where Checkbox IN ($querystr) 
    HAVING distance < :radius 
    ORDER BY distance ASC LIMIT 0, 50 " );

// Bind a value for each key
foreach ($params as $key => &$val) {
    $sql->bindParam(
        $key,
        $val,      
        PDO::PARAM_STR 
    );
 }


You will have to build they query yourself.

$ids = array(1, 2, 3);
$querystr = implode(',', $ids);

$sql = $db->prepare( " SELECT id, attribution,...... .................... 
    FROM filterdaiict where Checkbox IN ($querystr) 
    HAVING distance < :radius 
    ORDER BY distance ASC LIMIT 0, 50 " );
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜