note 103891 added to function.fgetcsv
| From: | erelsgl at gmail dot com | Date: | Mon, 09 May 2011 12:57:11 +0000 |
| Subject: | note 103891 added to function.fgetcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-179561@lists.php.net to get a copy of this message | ||
Here is a function that accepts the path to a CSV file, and inserts all records to the given MySQL
table, paying attention to the column names:
<?php
function csv_file_to_mysql_table($source_file, $target_table, $max_line_length=10000) {
if (($handle = fopen("$source_file", "r")) !== FALSE) {
$columns = fgetcsv($handle, $max_line_length, ",");
foreach ($columns as &$column) {
$column = str_replace(".","",$column);
}
$insert_query_prefix = "INSERT INTO $target_table
(".join(",",$columns).")\nVALUES";
while (($data = fgetcsv($handle, $max_line_length, ",")) !== FALSE) {
while (count($data)<count($columns))
array_push($data, NULL);
$query = "$insert_query_prefix
(".join(",",quote_all_array($data)).");";
mysql_query($query);
}
fclose($handle);
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 132.70.6.95
----
Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php
Edit -- https://master.php.net/note/edit/103891
Del: integrated -- https://master.php.net/note/delete/103891/integrated
Del: useless -- https://master.php.net/note/delete/103891/useless
Del: bad code -- https://master.php.net/note/delete/103891/bad+code
Del: spam -- https://master.php.net/note/delete/103891/spam
Del: non-english -- https://master.php.net/note/delete/103891/non-english
Del: in docs -- https://master.php.net/note/delete/103891/in+docs
Del: other reasons-- https://master.php.net/note/delete/103891
Reject -- https://master.php.net/note/reject/103891
Search -- https://master.php.net/manage/user-notes.php