Mysqli并在插入期间绑定多个值集 [英] Mysqli and binding multiple value sets during insert
问题描述
希望有人可以在这里给我一些见识.
Hoping someone can give me some insight here.
当必须一次向一个表中插入多行时,我使用了如下所示的sql:
When having to insert multiple rows into a table at once, I've used sql that looks something like this:
INSERT INTO some_names (firstName, lastName) VALUES ('Joe', 'Smith'),('Fred','Sampson'),('Lisa','Pearce')
如您所见,我正在用一条语句插入三行.我这样做的原因是,我认为它比执行三个不同的语句插入行更有效.
As you can see I'm inserting three rows with one statement. The reason I do this is that I believe it is more efficient than executing three distinct statements to insert the rows.
所以我的问题是这样的:如果我希望能够将我的值绑定到一条语句,该怎么办?不幸的是,我一直在网上浏览所有内容,但只能找到以下形式的单个语句示例:
So my question is this: how do I do this if I want to be able to bind my values to a statement? Unfortunately I've been looking all over the web and I can only find example of single statements in the form of:
$stmt = $mysqli->prepare("INSERT INTO some_names (firstName, lastName) VALUES (?, ?)");
$stmt->bind_param('ss', $firstName, $lastName);
$firstName = "Joe";
$lastName = "Smith";
$stmt->execute();
$firstName = "Fred";
$lastName = "Sampson";
$stmt->execute();
这似乎等同于执行单独的INSERT语句,并且效率似乎较低.
It seems that this would be the equivalent of doing separate INSERT statements and seems to be less efficient.
所以我的问题是:有什么方法可以在多插入语句中进行绑定?请在这里教育我!谢谢
So my question is: Is there any way to bind in a multi-insert statement? Please educate me here! Thanks
推荐答案
简单:
$stmt = $mysqli->prepare("INSERT INTO some_names (firstName, lastName) VALUES (?, ?),(?,?),(?,?)")
$stmt->bind_param('ssssss', 'Joe', 'Smith','Fred','Sampson','Lisa','Pearce');
这篇关于Mysqli并在插入期间绑定多个值集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!