一种方法是使用对象进行插入。如果数量太大,这允许批量插入,最终插入可以在 destruct 方法中完成。
像这样的东西: -
<?php
$InsertClassExample = new InsertClassDemo($db);
foreach($link as $i)
{
//some stuff
$InsertClassExample->InsertItem($s, $data, $data2);
}
unset($InsertClassExample);
class InsertClassDemo
{
var $db = '';
var $InsertArray = array();
function __CONSTRUCT($db)
{
$this->db = $db;
}
function __DESTRUCT()
{
if (count($this->InsertArray) > 0)
{
$this->PerformInsert();
}
}
public function InsertItem($s, $data, $data2)
{
$this->InsertArray[] = "('" . $s . "', '" . $data . "', '" . $data2 ."'), ";
if (count($this->InsertArray) > 250)
{
$this->PerformInsert();
}
}
private function PerformInsert()
{
$query = "INSERT INTO table VALUES ".implode(",", $this->InsertArray);
if($this->db->query($query) === false)
{
die("Insert Into table Failed - ".$this->db->error());
}
$this->InsertArray = array();
}
}
?>