准备多个Insert mysqli [英] Prepared multiple Insert mysqli
问题描述
在关闭问题之前,请先阅读完整的问题:)
我正在使用 mysqli 寻找重要语句(重要,不是PDO,因为我无法使用它,并且无法将某些PDO代码传输到mysqli.),在这里我可以在长查询中插入许多价值(大约2000).但是查询必须准备.
所以我开始这样:
Please read the full question before closing it :)
Im looking for a prepared statement with mysqli (Important, not PDO, because I can't use it and can't transfer some PDO code to mysqli.), where i can insert on long query with a lot of values (about 2000). But the query has to be prepared.
So i started like that:
$array = array("a1", "a2", "a3","a5", "a7", "a5","a9", "a32", "a3", "a4");
// AND SO ON UP TO 2000
$type = "s";
$end = count($array);
$query = "INSERT INTO table (value) VALUES (?)";
for ($i = 0; $i <= $end - 1; $i++)
{
$query .= ", (?)";
$type .= "s";
}
$stmt = $conn->prepare($query);
$stmt->bind_param("$type", /* PROBLEM */); // HERE IS THE PROBLEM!!!
$stmt->execute();
$stmt->close();
但是现在我的问题是,如何动态地绑定"bind_param"中的变量?
请不要显示执行"上的for循环之类的信息,因为这对于2000次插入来说太慢了:).
But now my problem, how can I bind the variables in "bind_param" dynamicaly?
Please dont show me anything like for-loops on "execute", because this is too sloww for 2000 inserts :).
我的意思是
$allvalues = "";
foreach ($array as $value)
{
$allvalues .= "$value ";
}
$stmt->bind_param("$type", $allvalues);
但是,当然,我不能将其绑定.
But of course, I cant bind that.
推荐答案
使用 call_user_func_array
以使用数组调用该函数.
Use call_user_func_array
to call the function using an array.
或者只是传递所有变量以执行,那么您甚至不需要使用bind_param
Or just pass all of the variables through to execute, then you do not even need to use bind_param
这篇关于准备多个Insert mysqli的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!