简体   繁体   中英

Update mass data using PDO for mysql database

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

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM