DevSnips.com Code Snippet Repository
search:    


Navigation
  Home
About
Library
Contact
 
Snippet Library
  ColdFusion   338  
  ASP   201  
  PHP   101  
  HTML   11  
  JavaScript   77  
  XML   2  
  CSS   5  
  SQL   13  
  JSP   2  
  C#   1  
  ASP.NET   0  
  Submit a Code Snippet
 
Blog Archive
  September 2007
August 2007
July 2007
June 2007
May 2007
November 2006
October 2006
Search Archives
 
Random Affiliates
  Uno-Code
ReviewMe!
BioMetric Base
Tom Morris

Want to become an affiliate?
Read more...


Privacy Policy
© 2008

Blog Archive

 
How-to to dump line items from a text file into a MySQL database using PHP

Here is a quick how-to to dump line items from a text file into a database. You can also use INFILE for mysql if you're using CSV. This example is if you're needing to pull line items and then manipulate the data for whatever reason prior to inserting into a MySQL database.

This uses two important functions. fopen() and fgets(). fopen() will open the file. The 'r' flag is for 'reading'. Other possible parameters would include 'a' for 'appending' and 'w' for writing. After opening a file handle to the file, use fget() to grab the contents into a buffer. These will be individual lines in the file. My example explodes on the colon, but you could parse any data at this point and assign values to local variable.

<?
$DBname					= "dbname";
$DBuser					= "user";
$DBpassword				= "pass";
$DBserver				= "localhost";
if(!$connect			= mysql_connect($DBserver, $DBuser, $DBpassword)){
	echo "error";exit();
}

$DB					= mysql_select_db($DBname);
$handle				= fopen("/var/www/localhost/htdocs/test/test.txt","r");
while(!feof($handle)){
	$buffer			= fgets($handle,4096);
	$sections		= explode(":",$buffer);
	$itemOne		= $sections[0];
	$itemTwo		= $sections[1];
	$sql			= "UPDATE table
						SET itemOne = '".addslashes($itemOne)."'
						WHERE itemTwo = '".$itemOne."'";
	mysql_query($sql,$connect);
	echo $itemOne." ".$itemTwo."<br>";
}
echo "Done";
fclose($handle);
?>


Submitted on 11/09/06 at 11:39PM
Post Comment | Comments: 0
Bookmark to:
Add 'How-to to dump line items from a text file into a MySQL database using PHP' to Del.icio.us Add 'How-to to dump line items from a text file into a MySQL database using PHP' to digg Add 'How-to to dump line items from a text file into a MySQL database using PHP' to FURL Add 'How-to to dump line items from a text file into a MySQL database using PHP' to blinklist Add 'How-to to dump line items from a text file into a MySQL database using PHP' to reddit Add 'How-to to dump line items from a text file into a MySQL database using PHP' to Feed Me Links Add 'How-to to dump line items from a text file into a MySQL database using PHP' to Technorati Add 'How-to to dump line items from a text file into a MySQL database using PHP' to Yahoo My Web Add 'How-to to dump line items from a text file into a MySQL database using PHP' to Newsvine Add 'How-to to dump line items from a text file into a MySQL database using PHP' to Socializer Add 'How-to to dump line items from a text file into a MySQL database using PHP' to Ma.gnolia Add 'How-to to dump line items from a text file into a MySQL database using PHP' to Stumble Upon Add 'How-to to dump line items from a text file into a MySQL database using PHP' to Google Bookmarks Add 'How-to to dump line items from a text file into a MySQL database using PHP' to RawSugar Add 'How-to to dump line items from a text file into a MySQL database using PHP' to Squidoo Add 'How-to to dump line items from a text file into a MySQL database using PHP' to Spurl Add 'How-to to dump line items from a text file into a MySQL database using PHP' to BlinkBits Add 'How-to to dump line items from a text file into a MySQL database using PHP' to Netvouz Add 'How-to to dump line items from a text file into a MySQL database using PHP' to Rojo Add 'How-to to dump line items from a text file into a MySQL database using PHP' to Blogmarks Add 'How-to to dump line items from a text file into a MySQL database using PHP' to Shadows Add 'How-to to dump line items from a text file into a MySQL database using PHP' to Simpy Add 'How-to to dump line items from a text file into a MySQL database using PHP' to Co.mments Add 'How-to to dump line items from a text file into a MySQL database using PHP' to Scuttle

Go Back








Advertisements

TigerGPS