且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用准备好的语句一次在mysql数据库中插入多行?

更新时间:2023-12-03 14:24:16

这是完全有效的:

$stmt = $mysqli->prepare("INSERT INTO something (userid, time, title) VALUES (?, ?, ?)");

$stmt->bind_param('iis', $userid, time(), $title);
$stmt->execute();

$stmt->bind_param('iis', $userid, time(), $title);
$stmt->execute();

$stmt->bind_param('iis', $userid, time(), $title);
$stmt->execute();

$stmt->bind_param('iis', $userid, time(), $title);
$stmt->execute();

您可以遍历每次插入,绑定和执行的值数组.它不会像您链接的示例中的批量插入一样快,但是会更加安全.

You can foreach over your array of values to insert and bind and execute each time. It wont be quite as fast as the bulk insert in the example you linked, but it will be more secure.