Import CSV file into MySQL using PHP
![]() |
| Import CSV file into MySQL using PHP |
if you are beginners please follow the step..this is beginner friendly codes you will understand easily..
just follow the below below codes to Import CSV file into MySQL using PHP
Database:
1. Database name: codenair
2. table name importcsv
-- Table structure for table `importcsv`
CREATE TABLE IF NOT EXISTS `importcsv` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) NOT NULL, `email` varchar(100) NOT NULL, `department` varchar(100) NOT NULL, PRIMARY KEY(id) ) ENGINE=MyISAM AUTO_INCREMENT=522 DEFAULT CHARSET=latin1;
1. Create CSV file..
user.csv
"ANJAN KUMAR","Anjankumardhali6@gmail.com","Dept. of Physics" "Ratul Bain","ratul85@gmail.com","Computer Science" "Bonna","Bonna53@gmail.com","Software Engineer" "Rasel","rasel53@yahoo.com","Computer Programmer" "David Malan","David54@hotmail.com","Android Developer" "Kate","kate22@mail.com","Computer Engineer"
2. Create import.php file to import user.csv file into mysql
import.php
<?php
$conn=new mysqli('localhost','root','','codenair');
if(isset($_POST['submit'])){
$fileName = $_FILES["file"]["tmp_name"];
$file = fopen($fileName, "r");
if($file !== FALSE) {
while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
$name=$data[0];
$email=$data[1];
$department=$data[2];
//Inserting CSV Data into MYSQL
$result=$conn->query("INSERT INTO importcsv(name,email,department)VALUES('$name','$email','$department')");
if($result){
$msg='File Insert Successful';
}else{
$msg= 'Failed to Insert';
}
}
}else{
echo'Failed to open File';
}
?>
<?php
fclose($file);
}
?>
<html>
<head>
<title>Import CSV file into MySQL using PHP</title>
</head>
<!-- Upload Form-->
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" name="submit" value="Uplaod"/>
</form>
<?php if(isset($msg)){echo $msg;}?>
</html>
That's it Friends how to Import CSV file into MySQL using PHP..if you like this post please share with your friends ..Thank You..


No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.