Import CSV/Excel data into MYSQL database via PHP Script;
Posted by Raj
Import CSV/Excel data into MYSQL database via PHP Script;
In this article,I will explain How to import the CSV/Excel file in to MYSQL database via PHP Mysql Script.we can use PHP fgetcsv() function to parse/import data from CSV/Excel file into Mysql Database.
fetchCsv.php file will upload the CSV/Excel file and imort the file into Mysql Database.
please check following code:
Example:
fetchCsv.php
<?php
if(isset($_POST["Import"]))
{
$host="localhost"; // Host name.
$db_user="root";
$db_password="";
$db='myDB'; // Database name.
$conn=mysql_connect($host,$db_user,$db_password) or die (mysql_error());
mysql_select_db($db) or die (mysql_error());
echo $filename=$_FILES["file"]["tmp_name"];
//echo $ext=substr($filename,strrpos($filename,"."),(strlen($filename)-strrpos($filename,".")));
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
print_r($emapData);
$sql = "INSERT into myTable(name,address,email,password) values('$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]')";
mysql_query($sql);
}
fclose($file);
echo "CSV File has been successfully Imported";
}
else
echo "Invalid File:Please Upload CSV File";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Import CSV/Excel file</title>
</head>
<body>
<form enctype="multipart/form-data" method="post">
<table border="1" width="40%" align="center">
<tr >
<td colspan="2" align="center"><strong>Import CSV/Excel file</strong></td>
</tr>
<tr>
<td align="center">CSV/Excel File:</td><td><input type="file" name="file" id="file"></td></tr>
<tr >
<td colspan="2" align="center"><input type="submit" name="Import" value="Import"></td>
</tr>
</table>
</form>
</body>
</html>
fetchCsv.php file will upload the CSV/Excel file and imort the file into Mysql Database.
If you face any problem than let me know via comment .
In this article,I will explain How to import the CSV/Excel file in to MYSQL database via PHP Mysql Script.we can use PHP fgetcsv() function to parse/import data from CSV/Excel file into Mysql Database.
fetchCsv.php file will upload the CSV/Excel file and imort the file into Mysql Database.
please check following code:
Example:
fetchCsv.php
<?php
if(isset($_POST["Import"]))
{
$host="localhost"; // Host name.
$db_user="root";
$db_password="";
$db='myDB'; // Database name.
$conn=mysql_connect($host,$db_user,$db_password) or die (mysql_error());
mysql_select_db($db) or die (mysql_error());
echo $filename=$_FILES["file"]["tmp_name"];
//echo $ext=substr($filename,strrpos($filename,"."),(strlen($filename)-strrpos($filename,".")));
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
print_r($emapData);
$sql = "INSERT into myTable(name,address,email,password) values('$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]')";
mysql_query($sql);
}
fclose($file);
echo "CSV File has been successfully Imported";
}
else
echo "Invalid File:Please Upload CSV File";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Import CSV/Excel file</title>
</head>
<body>
<form enctype="multipart/form-data" method="post">
<table border="1" width="40%" align="center">
<tr >
<td colspan="2" align="center"><strong>Import CSV/Excel file</strong></td>
</tr>
<tr>
<td align="center">CSV/Excel File:</td><td><input type="file" name="file" id="file"></td></tr>
<tr >
<td colspan="2" align="center"><input type="submit" name="Import" value="Import"></td>
</tr>
</table>
</form>
</body>
</html>
fetchCsv.php file will upload the CSV/Excel file and imort the file into Mysql Database.
If you face any problem than let me know via comment .
This entry was posted on October 4, 2009 at 12:14 pm, and is filed under
how to fetch the mysql database into excel,
import csv data into mysql database,
PHP,
PHP interview questions and answers
.You can leave a response, or trackback from your own site.
March 11, 2013 at 12:55 PM
Put excel file with it to test you code.