如何在准备好的语句中处理多个查询?

时间:2017-02-06 07:16:33

标签: php mysql mysqli prepared-statement

我想通过循环绑定n个查询并在单个调用中执行查询。这有可能吗?

   foreach($values as $value){
    $stmt = $conn->prepare("insert into tablename (val) values (?)");
    $stmt->bind_param("i",$val);
}
$stmt->execute();

1 个答案:

答案 0 :(得分:0)

示例代码

$valArray = array();
foreach($values as $val)
{
    $valArray[] = "($val)";
}

$valString = implode(',', $valArray);

$stmt = $pdo->query("insert into tablename (val) values $valString");