我想使用pdo将海量数据添加到mysql数据库中
我想更新大约200行数据,每行10个字段,我在下面用了四个
连接到db很好,我测试了一些结果查询
使用foreach填充字段,工作也很好,我验证了内容
然而问题是执行功能,执行给出错误而没有插入数据
请做什么,特此呈现代码
//set db
$host = 'localhost';
$dbname = 'dbdata';
$attrs = array(PDO::ATTR_PERSISTENT => true);
$dbHandle = new PDO("mysql:host=$host;dbname=$dbname",'dbdatatable','tblpwd');
$dbHandle->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
//===================================================
//prepare sql
$sql = "UPDATE TickersList SET field1=?, field2=?, field3=? WHERE field4=?";
$STH = $dbHandle->prepare($sql);
//===================================================
//declare arrays
$ufield1 = array();
$ufiled2 = array();
$ufiled3 = array();
$ufiled4 = array();
//===================================================
//fill arrays with data
foreach( $this->_data as $qty ){
$ufield1 [] = $qty->data1;
$ufield2 [] = $qty->data2;
$ufield3 [] = $qty->data3;
$ufield4 [] = $qty->data4;
}
//===================================================
//execute query - not working :(
$STH->execute($sql,$ufield1,$ufield2,$ufield3);
//===================================================
答案 0 :(得分:0)
您对执行方法的调用错误且位置错误。你应该这样做:
//fill arrays with data
foreach( $this->_data as $qty ){
$params = array($qty->data1,$qty->data2,$qty->data3,$qty->data4);
$STH->execute($params);
}