【发布时间】:2026-01-27 17:30:01
【问题描述】:
这是一个来自 php.net 的 pdo 执行和提交示例
<?php
/* Begin a transaction, turning off autocommit */
$dbh->beginTransaction();
/* Insert multiple records on an all-or-nothing basis */
$sql = 'INSERT INTO fruit
(name, colour, calories)
VALUES (?, ?, ?)';
$sth = $dbh->prepare($sql);
foreach ($fruits as $fruit) {
$sth->execute(array(
$fruit->name,
$fruit->colour,
$fruit->calories,
));
}
/* Commit the changes */
$dbh->commit();
/* Database connection is now back in autocommit mode */
?>
据我了解,这使我能够更快地在表中插入数千行。现在,如果我有一个名为的 php 文件 http://localhost/insertFruit.php?name=x&color=y&calories=z 它基本上收集这些值并将它们插入数据库。
现在,如果我想利用该 PDO 循环并执行,我不能。如果有 100 个这些 insertFruit.php 提交,我如何收集所有这些值,组合成一个数组然后提交?使用单独的类、调用对象、使用全局值等?
我确信有更好的方法来做到这一点。
【问题讨论】:
-
为什么您认为它可以让您更快地在表中插入数千行?我想你可能会有点困惑,这不是它的工作原理。