PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

PHP Source Code


Welcome to Dream.In.Code
Become a PHP Expert!

Join 300,371 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,455 people online right now. Registration is fast and FREE... Join Now!





CSV to MySql

Snippet to take the contents from a CSV file and insert it into a MySQL database table.

Submitted By: PsychoCoder
Actions:
Rating:
Views: 373

Language: PHP

Last Modified: July 7, 2009
Instructions: Instructions are in the comments header of the snippet

Snippet


  1. /********************************************************
  2. ** function to import a CSV file into a MySql table. The
  3. ** function expects 7 parameters:
  4. **
  5. ** $file = The file to open
  6. ** $delimiter = What the file is delimited with
  7. ** $host = Host of the database
  8. ** $dbname = Name of the database we're using
  9. ** $username = The databases username
  10. ** $password = Users password
  11. ** $table = Name of the table we're inserting into
  12. **
  13. *********************************************************/
  14. function CsvToSql($file, $delimiter, $host, $dbname, $username, $password, $table)
  15. {
  16.         //first connection to our database
  17.         $conn = mysql_connect($host, $username, $password) or die();
  18.         //select our database
  19.         mysql_select_db($dbname);
  20.        
  21.         //make sure the file exists
  22.         if(!file_exist($file)
  23.         {
  24.                 echo 'The file name '.$file.' does not exist';
  25.         }
  26.         else
  27.         {
  28.                 $data = fopen($file, "r");
  29.                
  30.                 //now make sure the open worked
  31.                 if(!$data)
  32.                 {
  33.                         'The file was empty';
  34.                 }
  35.                
  36.                 //read the contents of the file int the $contents variable
  37.                 $contents = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  38.                
  39.                 //now close the file since we have it's contents
  40.                 fclose($file);
  41.                
  42.                
  43.                 //start a loop
  44.                 for($i = 0 ; $i < $size; i++)
  45.                 {
  46.                         //get the current line
  47.                         $line = trim($contents[$i], $delimiter);
  48.                        
  49.                         $split = explode($delimiter, $line);
  50.                        
  51.                         $import = "INSERT INTO ". $table . " values('" . implode("','", $split) . "')";
  52.  
  53.                         if(!mysql_query($import))
  54.                                 echo_mysql_error();                       
  55.                 }
  56.                
  57.         }
  58.        
  59.         mysql_close($conn);
  60. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month