PDO use quote with an array

不羁的心 提交于 2019-12-25 05:23:24

问题


I'm new in PDO

$sFields = "'".implode("', '", $fields)."'";
$sColumns = implode(", ", $columns);
$sql = "INSERT INTO $table ($sColumns) VALUES ($sFields)";

What is the shortest way to use PDO::quote on each value I want to insert.

I tried

$fields = array_map('$bdd->quote', $fields);

but it returns:

Warning: array_map() expects parameter 1 to be a valid callback, function '$bdd->quote' not found or invalid function name


回答1:


There is other way than concat sql string.

$sColumns = implode(", ", $columns);
$sFields = implode(',', array_fill(0, count($fields), '?'));
$sql = "INSERT INTO $table ($sColumns) VALUES ($sFields)";

$stmt = $pdo->prepare($sql);
$stmt->execute($fields);



回答2:


Try

$fields = array_map(array($bdd, 'quote'), $fields);


来源:https://stackoverflow.com/questions/11999343/pdo-use-quote-with-an-array

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