我有一个从远程源读取数据的curl脚本。下面是当前的代码:
function download_page($path){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$path);
    curl_setopt($ch, CURLOPT_FAILONERROR,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    $retValue = curl_exec($ch);                      
    curl_close($ch);
    return $retValue;
}
$sXML = download_page('http://remotepage.php&function=getItems&count=100&page=1');
$oXML = new SimpleXMLElement($sXML);
foreach($oXML->results->item->item as $oEntry){
    $insert_query = mysql_query("INSERT INTO tbl_item (first_name, last_name, date_added) VALUES ('" . $oEntry->firstname . "', '" . $oEntry->lastname . "', '" . date("Y-m-d H:i:s") . "')");
}然而,正如我想象的那样,脚本的插入速度非常慢,因为它会写入每个单独的记录。count变量是每个页面返回的记录数,page变量是一个简单的页面计数器。
我想知道是否有一种方法可以执行bulk insert语句一次插入所有100条记录。
提前谢谢。
发布于 2012-05-16 17:49:35
要进行大容量插入操作,您可以将脚本追加到一个变量中,并且可以将其作为一个整体运行,但为此,您必须使用mysqli_multi_query。检查此链接http://www.php.net/manual/en/mysqli.multi-query.php
https://stackoverflow.com/questions/10615762
复制相似问题