Insert data excel csv file to Mysql database table
Friends if you have an excel CSV file and you want to insert excel file data into the database. This tutorial will be most helpful for you.
You need to follow simple 2 processes.
1. make a CSV file with the field of table fields
2. using while loop we can get each record and insert it into the table.
First of all, I will describe a little about of excel CSV. You have to know what is excel CSV format. I am writing something about CSV format in the below, read the text.
Insert data excel csv file to Mysql database table
What is CSV Format?
The CSV Format is a simple file format used to store stratified data, such as a spreadsheet or database. The files in the CSV format can be imported to and exported from programs that store data in tables, such as Microsoft Excel.
Now insert CSV files data in the database. I have some example code, I would like to insert the example code below this text, follow the example code.
$con=mysqli_connect("localhost","root","","my_sql_test"); $file = fopen('employee.csv', "r"); $i=0; while(! feof($file)) { $dataInArray = fgetcsv($file); if($i!=0) { $qry = "INSERT INTO employees (employee_name, employee_salary) values( '".$dataInArray[0]."', ".$dataInArray[1].")"; if($con->query($qry)===FALSE) echo 'Data not inserted...'; } $i++; }