首页 > 代码库 > YII insert multiple records into a table

YII insert multiple records into a table

$values = array(array(1,2),array(3,4),array(5,6),);$nbValues = count($values);$sql = ‘INSERT INTO table_name (col_name1, col_name2) VALUES ‘;for ($i=0; $i < $nbValues; $i++) {     $sql .= ‘(:col1_‘.$i.‘, :col2_‘.$i.‘)‘;    if ($i !== ($nbValues-1))        $sql .= ‘,‘;}$command = Yii::app()->db->createCommand($sql);for ($i=0; $i < $nbValues; $i++) {     $command->bindParam(‘:col1_‘.$i, $values[$i][0], PDO::PARAM_INT);    $command->bindParam(‘:col2_‘.$i, $values[$i][1], PDO::PARAM_INT);}$command->execute();