Array in the parameter when creating PDO query

爱⌒轻易说出口 提交于 2021-02-09 20:33:30

问题


Can I use a few lines in the parameter
Example

SELECT id, city_id FROM an_objects WHERE city_id IN (:arrCity)

(:arrCity) (1,2,3,4,5,6)

But now I have done like this

SELECT id, city_id FROM an_objects WHERE city_id IN (:1p, :2p, :3p, ...... :100p)

And it's very bad


回答1:


<?php

private function PDOBindArray(&$poStatement, &$paArray){ 
    foreach ($paArray as $k=>$v) {
        @$poStatement->bindValue($k, $v[0], $v[1]);
    }      
}

// the array structure should now look something like this

$inputArray = array(
    ':arrcity' => array($email, PDO::PARAM_STR), 
    ':another_variable' => array($pass, PDO::PARAM_INT)
);
?>



回答2:


And how in that case to be a query?

WHERE city_id IN (:1p, :2p, :3p, ...... :100p)

In the query 100 parameters
In this method, for example, we have only 5 parameters

private function PDOBindArray(&$poStatement, &$paArray){ 
    foreach ($paArray as $k=>$v) {
        @$poStatement->bindValue($k, $v[0], $v[1]);
    }      
}


来源:https://stackoverflow.com/questions/13962266/array-in-the-parameter-when-creating-pdo-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!