eHow launches Android app: Get the best of eHow on the go.

How To

How to Import CSV file into your database using PHP

Member
By ashishnayyar
User-Submitted Article
(1 Ratings)
CSV to database Import
CSV to database Import
Hurricanesoftwares.com

PHP gives an ease when it comes to Import flat data (Data without hierarchical structure). If you are using XML to fetch and store data that is fine but what about the case when your client have excel sheet ready with multiple records to go into database. CSV (Comma Separated Values) format is a good candidate format to import or export your application data.

Difficulty: Moderate
Instructions

Things You'll Need:

  • PHP Editor (Dreamweaver)
  • MySQL
  • Apache
  1. Step 1

    Importing data from CSV files

    Since we have lines of values separated by commas, the easiest way to process them is to parse each line using the PHP explode() function:

  2. Step 2

    Dealing with special characters

    This simple solution will not work if you have a comma in a value, like for instance when the column is an address and it has a value is like "North street, 1″.

    In such cases, the column value in the CSV file is quoted to indicate that the data between quotes should be read as a single column. To deal with this situation, you can use a specially tailored regular expression or use the PHP fgetcsv() function.

  3. Step 3

    Importing CSV data directly into the database
    If you are importing data from a CSV file into a MySQL database, you import it directly into a database table. It will be much faster than processing it line by line using PHP. Fortunately, most relational database have tools for the bulk data import from CSV files. Load data command will help you here. For instance, in MySQL you can use LOAD DATA INFILE query statement.

    For functions or htmls please visit hurricanesoftwares.com

Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Computers Newsletters

Copyright © 1999-2010 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy .   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License. † requires javascript

eHow Computers
eHow_eHow Technology and Electronics