To Import csv file into mysql database using php script follow the below code:
For example: I have a .csv file data with this formate:-
(1) Indresh Singh, indresh@abc.com, 12345
(2) mukul kant, mukul.kant@abc.com, 12345
To insert this data into database, I am using the fallowing code below-
<?php
//Database connection
$db = mysql_connect("localhost", "root", "123456");
if(!$db){
echo 'Mysql Connection Error';
}
if(!mysql_select_db("import",$db)){
echo 'Database Not Present'.mysql_error();
}
$deleterecords = "TRUNCATE TABLE users";
//use for empty the table of its current records
mysql_query($deleterecords);
//Upload File
if (isset($_POST['submit'])) {
//Import the uploaded file into Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$importQuery = "INSERT INTO users(name,email,password) VALUES ('".$data[0]."','".$data[1]."','".$data[2]."')";
mysql_query($importQuery);
}
fclose($handle);
echo 'File Impot Successfully';
//view upload form
}else {
#Html file tag includes#
}
0 Comment(s)