使用PDO for mysql数据库更新海量数据

时间:2022-09-26 08:17:31

I would like to add mass data to a mysql database using pdo
I want to update about 200 rows of data, each row 10 fields, i used four here below
connection to db is fine, i tested some result queries
fields are filled using foreach, which works also fine, i verified content
the problem however is the execute function, the execution gives an error and no data is inserted

我想使用pdo将大量数据添加到mysql数据库我要更新大约200行数据,每行10个字段,我在下面使用四个连接到db很好,我测试了一些结果查询字段是用foreach填充的,哪个工作也很好,我验证内容问题然而是执行功能,执行给出错误,没有插入数据

what to do please, hereby present code

请做什么,特此提出代码

//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);
//===================================================

1 个解决方案

#1


0  

Your call to execute method is wrong and at wrong place. You should do it like this:

您对执行方法的调用是错误的并且位置错误。你应该这样做:

//fill arrays with data
foreach( $this->_data as $qty ){

$params = array($qty->data1,$qty->data2,$qty->data3,$qty->data4);
$STH->execute($params);

}

#1


0  

Your call to execute method is wrong and at wrong place. You should do it like this:

您对执行方法的调用是错误的并且位置错误。你应该这样做:

//fill arrays with data
foreach( $this->_data as $qty ){

$params = array($qty->data1,$qty->data2,$qty->data3,$qty->data4);
$STH->execute($params);

}