|
||||
| Register--Login--Top 20 Posters--Search Topics |
Forum Main>>Tutorials>>Importing csv data into MySQL with php script | ||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 PM [Chipmunk] View Member Photo | Last replied to on Mon Aug 16, 2010 07:46:07 Edit Post|Quote This tutorial will show you how to import csv data into MYSQL with a simple PHP script. Many times you have to write a script because the import function in phpMyAdmin will not import the way you want. First you will need a connection into the MYSQL database in connect.php:
This is a basic database connector, insert your username, password, and dataname where mentioned. Now we need to do a few steps to import: 1. Open the CSV file to import 2. Read the data line by line 3. Store each line of data into a coma-delimited array 4. Insert individual pieces of the array into the desired SQL fields So here's the code in the import.php file to do that:
So if it does not detect a submit button, it simply prints out an form asking you to type in the uploaded CSV file to import. For this particular example to work, the .CSV file needs to be in the same directory as import.php and needs to have read permissions on. If your .CSV file is called file.csv, then type file.csv in the text box. After you push submit it create a variable $handle that contains the actual .CSV file. fopen simply opens the file you specified for reading. The while loop says that as long as there are more lines, read the file line by line, store each line into an array called $data. Note the parameters in this example specify that each row of data is at most 1000 characters long and is coma delimited by field. If your data rows are more than 1000 characters long or are not delimited by coma, you can simple change those two fields. For this example I'm using a MYSQL table called info with the fields name,address, and food. Now the data in each row is stored lik this: The data before the first coma is stored in $data[0], the data between the first and second coma is stored in $data[1], the data between the second and third coma is stored in $data[2] and so on. Now the actual SQL insertion:
This inserts the data stored in $data[0] into name, $data[1] into address, and $data[2] into food. Note that elements of the array do not have to be inserted in order. If food was the first element in the array and name was the third, the insertion would look like this instead:
Thats it for .CSV imports into MYSQL via PHP. You can extrapolate this for however many fields of data and MYSQL table fields you need. ----------------------------- Chipmunk, Supreme Administrator | |||||
| gladz Rank:acorn Group: members Posts: 2 IP Logged PM ID and RPS ID: 15344 [PM gladz] RPS score: 0 RPS challenge | Posted at Sat Mar 01, 2008 20:04:47 Edit post|Quote
| |||||
| gladz Rank:acorn Group: members Posts: 2 IP Logged PM ID and RPS ID: 15344 [PM gladz] RPS score: 0 RPS challenge | Posted at Sat Mar 01, 2008 20:07:12 Edit post|Quote [quote] This tutorial will show you how to import csv data into MYSQL with a simple PHP script. Many times you have to write a script because the import function in phpMyAdmin will not import the way you want. i just would like to ask.. how do you convert an excel file to .csv? i've tries searching for procedures how but i just couldn't fine one... pls help.. | |||||
| georgechemy Rank:acorn Group: members Posts: 1 IP Logged PM ID and RPS ID: 24684 [PM georgechemy] RPS score: 0 RPS challenge | Posted at Thu Oct 08, 2009 04:54:43 Edit post|Quote MySQL is a relational database management system (RDBMS) which has more than 6 million installations. MySQL stands for "My Structured Query Language". The program runs as a server providing multi-user access to a number of databases. MySQL is officially pronounced /maɪˌɛskjuːˈɛl/ My S-Q-L, but often pronounced /maɪˌsiː'kwɛl/ My SEQuel.The project has made its source code available under the terms of the GNU General Public License, as well as under a variety of proprietary agreements. MySQL is owned and sponsored by a single for-profit firm, the Swedish company MySQL AB, now a subsidiary of Sun Microsystems. As of 2009 Oracle Corporation began the process of acquiring Sun Microsystems; Oracle holds the copyright to most of the MySQL codebase.MySQL commonly features as part of free software projects which require a full-featured database management system, such as WordPress, phpBB and other software built on the LAMP software stack. It is also used in very high-scale World Wide Web products including Wikipedia, Google and Facebook. ----------------------------- hotels - travel - cheap flights | |||||
| Alex222 Rank:acorn Group: members Posts: 1 IP Logged PM ID and RPS ID: 31451 [PM Alex222] RPS score: 0 RPS challenge | Posted at Mon Aug 02, 2010 04:07:18 Edit post|Quote You can extrapolate this for about abounding fields of abstracts and MYSQL table fields you need. ----------------------------- Learn how to accomplish absorbing Website Designs and Best Web Designs again accomplish the best of it. | |||||
| ping123 Rank:acorn Group: members Posts: 10 IP Logged PM ID and RPS ID: 31806 [PM ping123] RPS score: 0 RPS challenge | Posted at Mon Aug 16, 2010 07:46:07 Edit post|Quote This tutorial be going to exhibit you how to importation csv accumulation into MYSQL with a ultimate PHP script. HH0-120 dumps Many nowadays you have to indite a playscript because the importation duty in phpMyAdmin be going to not import the HH0-120 exam artefact you want. i meet would alike to ask.. HH0-120 questions how do you modify an surpass enter to .csv? i've tries intelligent for procedures how but i meet couldn't dustlike one... plz help.. | |||||
Page: 1 |