mysql - Import particular Colums of csv in database in php/sql -
i have csv contains 15 columns.
but want import 2 columns csv in database.
whats should do?
i tried importing it's importing columns.
<?php //database connection details $connect = mysql_connect('localhost','root','123456'); if (!$connect) { die('could not connect mysql: ' . mysql_error()); } //your database name $cid =mysql_select_db('test',$connect); // path csv file located define('csv_path','c:/wamp/www/'); // name of csv file $csv_file = csv_path . "test.csv"; if (($handle = fopen($csv_file, "r")) !== false) { fgetcsv($handle); while (($data = fgetcsv($handle, 1000, ",")) !== false) { $num = count($data); ($c=0; $c < $num; $c++) { $col[$c] = $data[$c]; } $col1 = $col[0]; $col2 = $col[1]; $col3 = $col[2]; // sql query insert data database $query = "insert csvtbl(id,name,city) values('".$col1."','".$col2."','".$col3."')"; $s = mysql_query($query, $connect ); } fclose($handle); } echo "file data imported database!!"; mysql_close($connect); ?>
Comments
Post a Comment