nick Posted May 26, 2011 Share Posted May 26, 2011 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 More sharing options...
Paul Swanson Posted May 26, 2011 Share Posted May 26, 2011 The PHP Manual page for fgetcsv() includes a user note (the first one) that seems to cover exactly what you are wanting to do. 1 Link to comment Share on other sites More sharing options...
nick Posted May 26, 2011 Author Share Posted May 26, 2011 So instead of read print I would read insert from that example Link to comment Share on other sites More sharing options...
Paul Swanson Posted May 26, 2011 Share Posted May 26, 2011 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. 1 Link to comment Share on other sites More sharing options...
Recommended Posts