Page 1 of 1
uploading in mysql
Posted: Sat Apr 21, 2012 11:44 am
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.
Re: uploading in mysql
Posted: Sat Apr 21, 2012 3:09 pm
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);
}
?>
Re: uploading in mysql
Posted: Sat Apr 21, 2012 10:18 pm
by califdon
Check out the MySQL command LOAD DATA INFILE LOCAL ...
http://dev.mysql.com/doc/refman/5.1/en/load-data.html.
Re: uploading in mysql
Posted: Mon Apr 23, 2012 1:08 pm
by x_mutatis_mutandis_x
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
Re: uploading in mysql
Posted: Mon Apr 23, 2012 1:51 pm
by califdon
Good observation, thanks for pointing that out. I agree with your advice.