了解一个编程 API 的最好方式就是使用它,因此我们来看一下附带的这个演示,以了解如何进行批次更新(代码如下)。
<?php
// Create a PDO database handle object
// the 'oci:' string specifies that the OCI driver should be used
// you could use 'oci:dbname=name' to specify the database name.
// The second and third parameters are the username and password respectively
$dbh = new PDO('oci:', 'scott', 'tiger');
// Create a test table to hold the data from credits.csv
$dbh->exec("
CREATE TABLE CREDITS (
extension varchar(255),
name varchar(255)
)");
// start a transaction
$dbh->beginTransaction();
// prepare to insert a large quantitiy of data
$stmt = $dbh->prepare("INSERT INTO CREDITS (extension, name) VALUES (:extension, :name)");
// bind the inputs to php variables; specify that the data will be strings
// with a maximum length of 64 characters
$stmt->bindParam(':extension', $extension, PDO_PARAM_STR, 64);
$stmt->bindParam(':name', $name, PDO_PARAM_STR, 64);
// Open the .csv file for import
$fp = fopen('credits.csv', 'r');
while (!feof($fp)) {
list($extension, $name) = fgetcsv($fp, 1024);
$stmt->execute();
}
fclose($fp);
现在我要讲述一些技巧,如果您需要最后再调整一下脚本性能的话,这些技巧可能会对您有所帮助。但先给你一个忠告:要像躲避瘟疫一样避免不成熟的优化。您应该总是首选最清晰、可维护性最好的解决方案。请记住,在一个典型的 Web 应用程序中,您不能衡量各种抓取模式间的区别,除非脚本要处理很多行。我再重复一遍:抓取模式间的性能区别非常小 - 请使用最适合您代码的模式。