PHP CSVImport data -
i created file called import.php php used import csv file. not working fine.i have 3 field in database.name id email email got alwyas numeric value 0 , name null.
here part of script.
any ideas why being read long line?
<?php $connect = mysql_connect('localhost','root',''); if (!$connect) { die('could not <span class="il_ad" id="il_ad3">connect to</span> <span class="il_ad" id="il_ad12">mysql</span>: ' . mysql_error()); } $cid =mysql_select_db('test',$connect); // supply database name define('csv_path','d:/xampp/htdocs/import/'); // path csv file located $csv_file = csv_path . "aa.csv"; // name of csv file $csvfile = fopen($csv_file, 'r'); $thedata = fgets($csvfile); $i = 0; while (!feof($csvfile)) { $csv_data[] = fgets($csvfile, 1024); $csv_array = explode(",", $csv_data[$i]); $insert_csv = array(); $insert_csv['id'] = $csv_array[0]; $insert_csv['name'] = $csv_array[1]; $insert_csv['email'] = $csv_array[2]; $query = "insert csv(id,name,email) values('','".$insert_csv['name']."','".$insert_csv['email']."')"; $n=mysql_query($query, $connect ); $i++; } fclose($csvfile); echo "file data imported database!!"; mysql_close($connect); ?> , input file upload csv file....i got warning msg , data base fiel id created , email value got 0 , name id null. <html> <body> <form action="impt.php" method="post" enctype="multipart/form-data"> <label for="file">filename:</label> <input type="file" name="file" id="file"><br> <input type="submit" name="submit" value="submit"> </form> </body> </html>
this code using fgetcsv
<?php $connect = mysql_connect('localhost','root',''); if (!$connect) { die('could not <span class="il_ad" id="il_ad3">connect to</span> <span class="il_ad" id="il_ad12">mysql</span>: ' . mysql_error()); } $cid = mysql_select_db('test', $connect); // supply database name define('csv_path','d:/xampp/htdocs/import/'); // path csv file located $csv_file = csv_path . "aa.csv"; // name of csv file $csvfile = fopen($csv_file, 'r'); $i = 0; while (($data = fgetcsv($csvfile, 1000)) !== false) { if ($data != null) { // skip empty lines $insert_csv = array(); $insert_csv['id'] = $data[0]; $insert_csv['name'] = $data[1]; $insert_csv['email'] = $data[2]; $query = "insert csv(id,name,email) values('','".$insert_csv['name']."','".$insert_csv['email']."')"; $n=mysql_query($query, $connect ); } $i++; } fclose($csvfile); echo "file data imported database!!"; mysql_close($connect); ?>
Comments
Post a Comment