Importing CSV file to mysql table using "LOAD DATA" command.

hi, Here i am giving you some simple step to import huge data from a csv file to your mysql table.this is useful when you have a very huge data to import from csv format.

Imagine that you have a table, tbl_user(id(int AI),


in your db and you have a csv file like this format:Download test format.

step1:Define a Form model ie

class UserImportForm extends CFormModel
	public $file;
	 * @return array validation rules for model attributes.
	public function rules()
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(  
			 array('file', 'file', 
                                            'maxSize'=>1024 * 1024 * 10, // 10MB
                                            'tooLarge'=>'The file was larger than 10MB. Please upload a smaller file.',
                                            'allowEmpty' => false

	 * @return array customized attribute labels (name=>label)
	public function attributeLabels()
		return array(
			'file' => 'Select file',


Now you need to define a form in your

Note: i have used bootstrap form widget can change it based on your needs.

<div class="form">

$form = $this->beginWidget('bootstrap.widgets.BootActiveForm', array(
)); ?>

			<p class="note">Fields with <span class="required">*</span> are required.</p>
		<?php echo $form->errorSummary($model, 'Opps!!!', null, array('class'=>'alert alert-error span12')); ?>
		<div class="control-group">		
			<div class="span4">
                              	<div class="control-group <?php if ($model->hasErrors('postcode')) echo "error"; ?>">
		<?php echo $form->labelEx($model,'file'); ?>
		<?php echo $form->fileField($model,'file'); ?>
		<?php echo $form->error($model,'file'); ?>


		<div class="form-actions">
			<?php $this->widget('bootstrap.widgets.BootButton', array('buttonType'=>'submit', 'type'=>'primary', 'icon'=>'ok white', 'label'=>'UPLOAD')); ?>
			<?php $this->widget('bootstrap.widgets.BootButton', array('buttonType'=>'reset', 'icon'=>'remove', 'label'=>'Reset')); ?>

<?php $this->endWidget(); ?>

</div><!-- form -->

step3:Now you need to write an action in your controller to import the

public function actionImportCSV()
           $model=new UserImportForm;


                    $sql="LOAD DATA LOCAL INFILE '".$tempLoc."'
        INTO TABLE `tbl_user`
            TERMINATED BY ','
            ENCLOSED BY '\"'
            TERMINATED BY '\n'
         IGNORE 1 LINES
        (`name`, `age`, `location`)

                            catch(Exception $e) // an exception is raised if a query fails