Jump to content
Larry Ullman's Book Forums

Topic Covered On Fgetcsv()


Recommended Posts

I was reading through the index and found this function fgetcsv(). I was just wondering how much detail there was on this subject. I am have several of your books including the effortless eCommerce and the advanced php 5 book(Plus one more PHP 6 and MySQL 5). The reason I am asking is because I have need to use that function to extract values and insert them into a MySQL database every so often and want to do it dynamically. I would like to buy your new book but I just wasn't sure how much it covered on that topic or if the books I have covered anything on it that I missed.

Link to comment
Share on other sites

I was referring to the user contributed comments. The first one, from erelsgl at gmail dot com on May 9th, has this function:

<?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); 
} 
} 
?>

Looks like it will read your csv file, and insert the info to a database table.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...