Get your freak on with SSL and Apache :P Nice crazy game | cool arcade flash
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:

    i try your code getting error

    Fatal error: Class ‘mysqli’ not found in /home/mmstock/public_html/php/csv2database/example.php on line 10

  5. parkum Says:

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

  6. 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 :)

  7. Georgi Mitev Says:

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

  8. moustafa adel Says:

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

  9. erick Says:

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

  10. Nulled Scripts Says:

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

Leave a Reply

Spam Protection by WP-SpamFree

  • generic name viagra
  • viagra trial pack
  • european online pharmacy
  • new stop smoking medication
  • health vitamins
  • online discount pharmacy
  • pet ear infection remedies
  • pain management drugs
  • nerve pain remedy
  • organic vitamins
  • antibiotic overdose
  • how to lower high blood pressure
  • bone muscle pain
  • cancer medicine
  • effexor dosages
  • latest treatment for heart attack
  • buy brand names drugs
  • allegra drug
  • heart chest pain
  • bactrim online
  • tretinoin retin a
  • discount medication
  • clonazepam treatment
  • genaric viagra
  • taking diflucan
  • what can help heart failure
  • no prescription pain
  • how to relieve back pain
  • levitra side affects
  • dog health advice
  • leukemia medications
  • congestive heart failure online
  • help reduce high blood pressure
  • levitra warfarin
  • free ultram
  • treating aids in africa
  • how can i stop smoking
  • on-line drugs
  • benadryl dosage
  • prevention of hypertension
  • buy cialis online viagra
  • cialis herbal
  • flu shot
  • buy cialis tadalafil
  • what is elavil
  • weight loss information
  • male enhancements
  • aspirin therapy
  • strengthen immune system
  • viagra women
  • dog antibiotics
  • us online pharmacies
  • nolvadex generic
  • benicar generic
  • do diet pills work
  • parasite killer human
  • treatment of chlamydia
  • what valium does
  • weight loss stuff for free
  • cialis cheapest
  • natural health for dogs
  • arthritis pain medicine
  • gout cures
  • high blood pressure prevention
  • cla buy
  • pet ear infection remedies
  • generic 5mg proscar cheap
  • cheap impotence drug generic cialis delivery
  • weight loss pill that works
  • allergy medication
  • new weight loss drug
  • natural antibiotic anti-biotic
  • diabetes prevention
  • rheumatoid arthritis medications
  • medication gonorrhea
  • alzheimers meds
  • cialis benefits
  • medications ativan
  • drug hydroxyzine
  • meds without prescriptions
  • mens health supplements
  • baby acne
  • birth control online
  • pet supplies products
  • do diet pills work
  • cheap viagra soft
  • nexium use
  • cialis versus viagra
  • female health problems
  • abnormal blood clots
  • buy cialis without a prescription
  • breast augmentation info
  • new drugs for depression
  • arthritis pain medicine
  • cholesterol canada
  • what is hoodia
  • osteoporosis help
  • abscess infection cats
  • health products for men
  • treatment for high blood pressure
  • levitra generic
  • treatment of breast cancer
  • mobic tablets
  • strattera pharmacy
  • drug sarafem
  • carisoprodol dosage
  • women's health tips
  • viagra from usa
  • chest pain
  • allergy medicine
  • blood clots lungs
  • buy drugs online
  • teeth whitening methods
  • buying prescription drugs online
  • how to lower blood sugar
  • parkinson's drugs
  • lexapro info
  • asthma asthma
  • cialis 50mg
  • new levitra
  • severe hair loss
  • pain medication safe while pregnant
  • male health vitamins
  • otc sleep aids
  • vitamin b-12
  • high blood pressure treatment
  • carisoprodol medication
  • living with heart failure
  • diabetes cure
  • women body building
  • cat's health
  • buspirone dosage
  • diet for high blood pressure
  • generic viagra lowest price
  • bupropion 150 mg
  • information on gout
  • what does clomid do
  • medication price
  • weight loss tablet
  • top weight loss drugs
  • discount skin care
  • amitriptyline uses
  • where to buy cialis
  • online cheap soma
  • order alli
  • stomach pain
  • xanax price
  • buy canadian drugs
  • bronchitis medications
  • weight loss tablet
  • pet safety products
  • preventing bone loss
  • advice care health pet uk
  • discount pharmacies in canada
  • natural sleep aid
  • super flu
  • prostate cancer treatment
  • treating dependent edema
  • herbs for impotence
  • wrinkle reducers
  • cheapest cialis generic
  • cheapest cialis uk
  • dog health care
  • cialis 5
  • smoking cessation programs
  • generic online ultram
  • adhd in children
  • valium high
  • pet safety products
  • medicines for insomnia
  • human parasite
  • simvastatin tablets
  • free diabetes products
  • online prescription pharmacy
  • pet med
  • purchase vitamins
  • penis enlargement result
  • medication skelaxin
  • buy body building supplement
  • burning fat
  • left side pain
  • depression drugs
  • pain drugs
  • tramadol and dosage
  • discount weight loss prescription
  • weight loss exercise tips
  • drugs online
  • teeth white
  • stop smoke
  • sildenafil online
  • professional viagra
  • blockers calcium channel
  • avapro 150mg
  • fda levitra
  • blue pills
  • pain medicine online ordering
  • male health drugs
  • chronic congestive heart failure
  • normal blood pressure
  • stronger erections