Mar 09

Hello everybody,

today i’ll be talking about how to export / import your Excel file into you your database using the PHP and the CSV file format.
First of all we need an Excel file with our data.

Every row of the excel file should be the same format as the previous row (so that we can make our life easier), for example if we want to import a excel file with name/url addres corresponding to this name we should have our excel file in a format like this:
exel file example
After u have the file in this excel format u should hit -> File -> Save as -> CSV (Comma Delimited) or more ofter its Called “Comma Separated Values”. For the comma separator use “;” if it asks you. So after you save the file in the CSV file format you should have something like this after you open the file with notepad etc.:


exel file example
in this case we use “;” for comma separator.

ok lets get now to the Databse – in my case i will be using mysql, so for the sample excel file above you can use this code to create your database:

CREATE DATABASE MerchantManager;
USE MerchantManager;
CREATE TABLE IF NOT EXISTS `merchants` (
`MerchantID` int(11) NOT NULL AUTO_INCREMENT,
`MerchantTimestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`MerchantName` varchar(256) NOT NULL,
`MerchantURL` varchar(256) NOT NULL,
PRIMARY KEY (`MerchantID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=185 ;

after we create the database we are going to write some function in php that can import the data into our Database:

we can create now a new file – “import.php”

<?

function AddMerchant($name, $url){
$host = “localhost”;
$user = “root”;
$pass = “”; // use your mysql password
$db_name = “merchantmanager”;
$table = “merchants”;

mysqli_query(new mysqli($host, $user, $pass, $db_name), “INSERT INTO “.$table.” (`MerchantName`, `MerchantURL`) VALUES (‘$name’, ‘$url’)”);

}
?>

:) okz i hope it’s all clear for now, so let’s get to the php now :)
To export the CSV file into our database we are going to use:

“file_get_contents” function – http://bg.php.net/file_get_contents
and “explode” function – http://bg.php.net/manual/en/function.explode.php

so lets get started, after the “AddMerchant()” function that we wrote above, we will add the following code into the file “import.php”

$file = file_get_contents(“file.csv”); //we load the CSV file and put it in a variable

$rows = explode(“\n”, $file); //we brake the variable into array, using the new line as a condition

foreach($rows as $row){

$items = explode(“;”, $row); //we brake it again into another array using the “;” sign in our case
AddMerchant($items[0],$items[1]);// we get the first and the second value of the array (name and url)
echo “success”;

}

?>

Can you believe it’s that simple ;) 3 lines of code and we managed to put the information from the excel file into the database.

if you want to get all the source code from this simple tutorial
download it here: export_csv_to_mysql.rar

10 Responses to “Export / Import Excel file to sql / database using CSV and PHP”

  1. Raj Says:

    You made my day ..Thanks Buddy

  2. neal99 Says:

    thanx buddy ……..

  3. Abi Says:

    i have upload CSV file to mySQl Database now how can i link to it to my website …..i mean how can i make search bar for my website…and how do i link it to that CSV file

  4. parkum Says:

    Fatal error: Call to undefined function mysqli_query() in /home/mmstock/public_html/php/csv2database/example.php on line 10

  5. Georgi Mitev Says:

    Hi parkum
    first enter and find where your php.ini is located.
    after that open your php.ini file and find this line:
    ;extension=php_mysqli.dll
    uncomment this line (remove the “;” sign)
    restart your apache server
    you should be ok now :)

  6. Georgi Mitev Says:

    enter phpinfo(); to find where is your php.ini :)

  7. moustafa adel Says:

    thanx alot
    that’s what i was looking for
    i wish u all the best
    once again thanx

  8. erick Says:

    how can i insert a data in the xampp database using excel??? wat are the codes?? pls help… thanx…

  9. Nulled Scripts Says:

    Excellent post..Keep them coming :) Thanks for sharing.

  10. Christian Louboutin Sale Says:

    Dresses for juniors are typically for prom and there are many dress outlets that specialize in prom sarongs and other formal gowns.

Leave a Reply

*

Spam Protection by WP-SpamFree