uploading in mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jamhussain
Forum Newbie
Posts: 17
Joined: Sat Apr 07, 2012 2:04 am

uploading in mysql

Post by jamhussain »

I have a file csv or xml.
it has three fields
country name, birth rate, year,

I want to upload it in mysql database.
can any body provide me link of php code.
thanks.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: uploading in mysql

Post by litebearer »

example...

Code: Select all

<?PHP
    $new_file = “your folder /feed.csv”;

    $fcontents = file ($new_file);

    foreach($fcontents as $line) {
    $arr = explode( ‘,’, $line);
    }

    foreach($arr as $data) {

    $query = “insert into `your db table` VALUES(’$data’)”;

    mysql_query($query);

    }
?>

User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: uploading in mysql

Post by califdon »

Check out the MySQL command LOAD DATA INFILE LOCAL ... http://dev.mysql.com/doc/refman/5.1/en/load-data.html.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: uploading in mysql

Post by x_mutatis_mutandis_x »

califdon wrote:Check out the MySQL command LOAD DATA INFILE LOCAL ... http://dev.mysql.com/doc/refman/5.1/en/load-data.html.
Lets you do it only from the database server's local filesystem.

Use fgetcsv. It lets you specify any delimiter for your csv file, since a csv file is not always delimited by ','.
http://php.net/manual/en/function.fgetcsv.php

For xml, you can use "simplexml_load_file"
http://us.php.net/manual/en/function.si ... d-file.php
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: uploading in mysql

Post by califdon »

x_mutatis_mutandis_x wrote:
califdon wrote:Check out the MySQL command LOAD DATA INFILE LOCAL ... http://dev.mysql.com/doc/refman/5.1/en/load-data.html.
Lets you do it only from the database server's local filesystem.

Use fgetcsv. It lets you specify any delimiter for your csv file, since a csv file is not always delimited by ','.
http://php.net/manual/en/function.fgetcsv.php

For xml, you can use "simplexml_load_file"
http://us.php.net/manual/en/function.si ... d-file.php
Good observation, thanks for pointing that out. I agree with your advice.
Post Reply