Jump to content
Larry Ullman's Book Forums

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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...