<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: parsing csv file with php and inserting into mysql database</title>
	<atom:link href="http://www.gsdesign.ro/blog/parsing-csv-file-with-php-and-inserting-into-mysql-database/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gsdesign.ro/blog/parsing-csv-file-with-php-and-inserting-into-mysql-database/</link>
	<description>Just another developer blog</description>
	<lastBuildDate>Sat, 11 Feb 2012 02:17:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Anonymous</title>
		<link>http://www.gsdesign.ro/blog/parsing-csv-file-with-php-and-inserting-into-mysql-database/comment-page-1/#comment-7950</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Thu, 26 Jan 2012 23:54:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.gsdesign.ro/blog/?p=59#comment-7950</guid>
		<description>I need to write a script to do the following.
Hopefully someone can help me with this. Seems pretty similar to the above, but I am still learning.
I need to write a script that does the following.1. read data from csv files in a bunch of directories (the list of directories will grow in time)2. the idea is that these csv files will be updated by car dealers and then uploaded to their respective directories thru ftp (I&#039;m good with this bit as I will set the dir&#039;s and access)3. problem comes in that at the dealer side the content of the files will keep changing. (some car info will remain / some will be deleted / and new ones will be added), this goes for all the dealers.4. With every csv file they will upload images relating to the data in the csv files, so I will need to link this as well somehow.5. the idea is to run this file with cron.php at set intervals to update the database (will ask how to do this when I get there)What I&#039;ve managed so far is to loop through the lot with the glob function, and adding the files, but my problem is updating the database. (cars removed from the csv files must not be deleted just set to sold, so we can keep that data for statistics)Hope someone can point me in the right direction.This is the code i&#039;ve written so far:PS: there are no headings in the csv files.&lt;?php$username =&quot;root&quot;;$password = &quot;myPasword&quot;;$host = &quot;localhost&quot;;$table = &quot;csv_table&quot;;$conn = new mysqli(&quot;$host&quot;, &quot;$username&quot;, &quot;$password&quot;);// echo &quot;Connected to localhost&quot; . &quot;&quot;;mysql_select_db(&quot;csvdb&quot;) or die(mysql_error());// echo &quot;Connected to Database&quot;;?&gt;&lt;?php// Set variable for csv file path $dir = &quot;dealer_upload/*/*.csv&quot;; // Open a known directory, and proceed to read its contents foreach(glob($dir) as $file) { //echo &quot;PATH AND FILENAME: &quot; . $file . &quot;&quot;;// Create the array$fileTemp = $file;$fp = fopen($fileTemp,&#039;r&#039;);$datas = array();while (($data = fgetcsv($fp)) !== FALSE){$stockNumber = trim($data[0]);$make = trim($data[1]);$model = trim($data[2]);$derivative = trim($data[3]);$series = trim($data[4]);$reg = trim($data[5]);$vin = trim($data[6]);$driveAwayPrice = trim($data[7]);$priceExcluding = trim($data[8]);$specialPrice = trim($data[9]);$year = trim($data[10]);$kilometres = trim($data[11]);$body = trim($data[12]);$colour = trim($data[13]);$engine = trim($data[14]);$transmission = trim($data[15]);$fuel = trim($data[16]);$options = trim($data[17]);$sellingPoints = trim($data[18]);$nvic = trim($data[19]);$redBook = trim($data[20]);// Insert Datamysql_query (&quot;INSERT INTO $table (id_dealer, stockNumber, make, model, derivative, series, reg, vin, driveAwayPrice, priceExcluding, specialPrice, year, kilometres, body, colour, engine, transmission, fuel, options, sellingPoints, nvic, redBook)VALUES (&#039;$file&#039;, &#039;$stockNumber&#039;, &#039;$make&#039;, &#039;$model&#039;, &#039;$derivative&#039;, &#039;$series&#039;, &#039;$reg&#039;, &#039;$vin&#039;, &#039;$driveAwayPrice&#039;, &#039;$priceExcluding&#039; ,&#039;$specialPrice&#039; , &#039;$year&#039; , &#039;$kilometres&#039; , &#039;$body&#039;, &#039;$colour&#039;, &#039;$engine&#039;, &#039;$transmission&#039;, &#039;$fuel&#039;, &#039;$options&#039;, &#039;$sellingPoints&#039;, &#039;$nvic&#039;, &#039;$redBook&#039;)&quot;)or die (mysql_error());} } ?&gt;</description>
		<content:encoded><![CDATA[<p>I need to write a script to do the following.<br />
Hopefully someone can help me with this. Seems pretty similar to the above, but I am still learning.<br />
I need to write a script that does the following.1. read data from csv files in a bunch of directories (the list of directories will grow in time)2. the idea is that these csv files will be updated by car dealers and then uploaded to their respective directories thru ftp (I&#8217;m good with this bit as I will set the dir&#8217;s and access)3. problem comes in that at the dealer side the content of the files will keep changing. (some car info will remain / some will be deleted / and new ones will be added), this goes for all the dealers.4. With every csv file they will upload images relating to the data in the csv files, so I will need to link this as well somehow.5. the idea is to run this file with cron.php at set intervals to update the database (will ask how to do this when I get there)What I&#8217;ve managed so far is to loop through the lot with the glob function, and adding the files, but my problem is updating the database. (cars removed from the csv files must not be deleted just set to sold, so we can keep that data for statistics)Hope someone can point me in the right direction.This is the code i&#8217;ve written so far:PS: there are no headings in the csv files.&lt;?php$username =&quot;root&quot;;$password = &quot;myPasword&quot;;$host = &quot;localhost&quot;;$table = &quot;csv_table&quot;;$conn = new mysqli(&quot;$host&quot;, &quot;$username&quot;, &quot;$password&quot;);// echo &quot;Connected to localhost&quot; . &quot;&#8221;;mysql_select_db(&#8220;csvdb&#8221;) or die(mysql_error());// echo &#8220;Connected to Database&#8221;;?&gt;&lt;?php// Set variable for csv file path $dir = &quot;dealer_upload/*/*.csv&quot;; // Open a known directory, and proceed to read its contents foreach(glob($dir) as $file) { //echo &quot;PATH AND FILENAME: &quot; . $file . &quot;&#8221;;// Create the array$fileTemp = $file;$fp = fopen($fileTemp,&#8217;r');$datas = array();while (($data = fgetcsv($fp)) !== FALSE){$stockNumber = trim($data[0]);$make = trim($data[1]);$model = trim($data[2]);$derivative = trim($data[3]);$series = trim($data[4]);$reg = trim($data[5]);$vin = trim($data[6]);$driveAwayPrice = trim($data[7]);$priceExcluding = trim($data[8]);$specialPrice = trim($data[9]);$year = trim($data[10]);$kilometres = trim($data[11]);$body = trim($data[12]);$colour = trim($data[13]);$engine = trim($data[14]);$transmission = trim($data[15]);$fuel = trim($data[16]);$options = trim($data[17]);$sellingPoints = trim($data[18]);$nvic = trim($data[19]);$redBook = trim($data[20]);// Insert Datamysql_query (&#8220;INSERT INTO $table (id_dealer, stockNumber, make, model, derivative, series, reg, vin, driveAwayPrice, priceExcluding, specialPrice, year, kilometres, body, colour, engine, transmission, fuel, options, sellingPoints, nvic, redBook)VALUES (&#8216;$file&#8217;, &#8216;$stockNumber&#8217;, &#8216;$make&#8217;, &#8216;$model&#8217;, &#8216;$derivative&#8217;, &#8216;$series&#8217;, &#8216;$reg&#8217;, &#8216;$vin&#8217;, &#8216;$driveAwayPrice&#8217;, &#8216;$priceExcluding&#8217; ,&#8217;$specialPrice&#8217; , &#8216;$year&#8217; , &#8216;$kilometres&#8217; , &#8216;$body&#8217;, &#8216;$colour&#8217;, &#8216;$engine&#8217;, &#8216;$transmission&#8217;, &#8216;$fuel&#8217;, &#8216;$options&#8217;, &#8216;$sellingPoints&#8217;, &#8216;$nvic&#8217;, &#8216;$redBook&#8217;)&#8221;)or die (mysql_error());} } ?&gt;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Poster</title>
		<link>http://www.gsdesign.ro/blog/parsing-csv-file-with-php-and-inserting-into-mysql-database/comment-page-1/#comment-7832</link>
		<dc:creator>Poster</dc:creator>
		<pubDate>Thu, 06 Jan 2011 15:09:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.gsdesign.ro/blog/?p=59#comment-7832</guid>
		<description>You don&#039;t need PHP to do this though, you can use MySQL Load Data Infile (or local infile) http://dev.mysql.com/doc/refman/5.1/en/load-data.html </description>
		<content:encoded><![CDATA[<p>You don&#8217;t need PHP to do this though, you can use MySQL Load Data Infile (or local infile) <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html" rel="nofollow">http://dev.mysql.com/doc/refman/5.1/en/load-data.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sana India Associate</title>
		<link>http://www.gsdesign.ro/blog/parsing-csv-file-with-php-and-inserting-into-mysql-database/comment-page-1/#comment-7815</link>
		<dc:creator>Sana India Associate</dc:creator>
		<pubDate>Mon, 11 Oct 2010 11:58:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.gsdesign.ro/blog/?p=59#comment-7815</guid>
		<description>Great Code</description>
		<content:encoded><![CDATA[<p>Great Code</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rhiane Caña</title>
		<link>http://www.gsdesign.ro/blog/parsing-csv-file-with-php-and-inserting-into-mysql-database/comment-page-1/#comment-7605</link>
		<dc:creator>Rhiane Caña</dc:creator>
		<pubDate>Thu, 18 Jun 2009 04:08:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.gsdesign.ro/blog/?p=59#comment-7605</guid>
		<description>Hi, Thanks a lot to your blog. It helps me to solve my csv upload. :)&lt;br&gt;&lt;br&gt;Because of your blog, I come up an idea for csv upload. I updated you code.&lt;br&gt;&lt;br&gt;&lt;?php &lt;br&gt;&lt;br&gt; 	$filename = $_FILES[&#039;uploadFile&#039;][&#039;name&#039;];&lt;br&gt;	$file_ext = pathinfo($filename, PATHINFO_EXTENSION);&lt;br&gt;	$file_path = &quot;uploads/&quot;.$filename;&lt;br&gt;&lt;br&gt;	if(strtoupper($file_ext)==&#039;CSV&#039;){&lt;br&gt;		if(move_uploaded_file($_FILES[&quot;uploadFile&quot;][&#039;tmp_name&#039;],$file_path)){&lt;br&gt;			&lt;br&gt;			$file1 = fopen($file_path, &#039;r&#039;);&lt;br&gt;			// initialize header...&lt;br&gt;			$ctr1 = 0;&lt;br&gt;			while(($data1 = fgetcsv($file1 , 1000, &quot;,&quot;)) !== FALSE){&lt;br&gt;				if($ctr1==0){&lt;br&gt;					foreach($data1 as $v1){&lt;br&gt;						$insert_headers[] = &quot;&#039;&quot;. addslashes(trim($v1)) .&quot;&#039;&quot;;				&lt;br&gt;					}&lt;br&gt;					$columns = implode(&#039;,&#039;, $insert_headers);&lt;br&gt;					unset($insert_headers);&lt;br&gt;					$ctr1 = 1;&lt;br&gt;				}&lt;br&gt;			}&lt;br&gt;			fclose($file1);&lt;br&gt;			&lt;br&gt;			&lt;br&gt;			$file = fopen($file_path, &#039;r&#039;);&lt;br&gt;			&lt;br&gt;			// initialize data...&lt;br&gt;			$ctr2 = 0;&lt;br&gt;			while(($data = fgetcsv($file , 1000, &quot;,&quot;)) !== FALSE){&lt;br&gt;				foreach($data as $v){&lt;br&gt;					$insert_values[] = &quot;&#039;&quot;. addslashes(trim($v)) .&quot;&#039;&quot;;			&lt;br&gt;				}&lt;br&gt;&lt;br&gt;				$values = implode(&#039;,&#039;, $insert_values);&lt;br&gt;				unset($insert_values);&lt;br&gt;				&lt;br&gt;				if($ctr2!=0){&lt;br&gt;					$sql_query = &quot;INSERT INTO &lt;br&gt;									tbl_article_info($columns) &lt;br&gt;								  VALUES($values)&quot;;&lt;br&gt;				}&lt;br&gt;				else{&lt;br&gt;					$ctr2 = 1;&lt;br&gt;				}&lt;br&gt;				&lt;br&gt;				mysql_query($sql_query) or die(&quot;Query Error:&quot; . mysql_error());&lt;br&gt;			}&lt;br&gt;			fclose($file);&lt;br&gt;&lt;br&gt;			echo &quot;success&quot;;&lt;br&gt;		}&lt;br&gt;		else{&lt;br&gt;			echo &#039;failed&#039;;&lt;br&gt;		}&lt;br&gt;	}&lt;br&gt;	else{&lt;br&gt;		echo &quot;THE FILE IS NOT IN CSV FORMAT.&quot;;&lt;br&gt;	}&lt;br&gt;?&gt;</description>
		<content:encoded><![CDATA[<p>Hi, Thanks a lot to your blog. It helps me to solve my csv upload. <img src='http://www.gsdesign.ro/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Because of your blog, I come up an idea for csv upload. I updated you code.</p>
<p>&lt;?php </p>
<p> 	$filename = $_FILES[&#39;uploadFile&#39;][&#39;name&#39;];<br />	$file_ext = pathinfo($filename, PATHINFO_EXTENSION);<br />	$file_path = &#8220;uploads/&#8221;.$filename;</p>
<p>	if(strtoupper($file_ext)==&#39;CSV&#39;){<br />		if(move_uploaded_file($_FILES["uploadFile"][&#39;tmp_name&#39;],$file_path)){</p>
<p>			$file1 = fopen($file_path, &#39;r&#39;);<br />			// initialize header&#8230;<br />			$ctr1 = 0;<br />			while(($data1 = fgetcsv($file1 , 1000, &#8220;,&#8221;)) !== FALSE){<br />				if($ctr1==0){<br />					foreach($data1 as $v1){<br />						$insert_headers[] = &#8220;&#39;&#8221;. addslashes(trim($v1)) .&#8221;&#39;&#8221;;				<br />					}<br />					$columns = implode(&#39;,&#39;, $insert_headers);<br />					unset($insert_headers);<br />					$ctr1 = 1;<br />				}<br />			}<br />			fclose($file1);</p>
<p>			$file = fopen($file_path, &#39;r&#39;);</p>
<p>			// initialize data&#8230;<br />			$ctr2 = 0;<br />			while(($data = fgetcsv($file , 1000, &#8220;,&#8221;)) !== FALSE){<br />				foreach($data as $v){<br />					$insert_values[] = &#8220;&#39;&#8221;. addslashes(trim($v)) .&#8221;&#39;&#8221;;			<br />				}</p>
<p>				$values = implode(&#39;,&#39;, $insert_values);<br />				unset($insert_values);</p>
<p>				if($ctr2!=0){<br />					$sql_query = &#8220;INSERT INTO <br />									tbl_article_info($columns) <br />								  VALUES($values)&#8221;;<br />				}<br />				else{<br />					$ctr2 = 1;<br />				}</p>
<p>				mysql_query($sql_query) or die(&#8220;Query Error:&#8221; . mysql_error());<br />			}<br />			fclose($file);</p>
<p>			echo &#8220;success&#8221;;<br />		}<br />		else{<br />			echo &#39;failed&#39;;<br />		}<br />	}<br />	else{<br />		echo &#8220;THE FILE IS NOT IN CSV FORMAT.&#8221;;<br />	}<br />?&gt;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: yanti</title>
		<link>http://www.gsdesign.ro/blog/parsing-csv-file-with-php-and-inserting-into-mysql-database/comment-page-1/#comment-7523</link>
		<dc:creator>yanti</dc:creator>
		<pubDate>Thu, 14 May 2009 07:31:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.gsdesign.ro/blog/?p=59#comment-7523</guid>
		<description>the code is like this,..

if(isset($_POST[&#039;submit&#039;]))
   {
$columns = &quot;no_ahli, no_pin, nama, no_ic_baru, no_ic_lama&quot;;
$handle = fopen(&quot;filename.csv&quot;, &quot;r&quot;);
while (($data = fgetcsv($handle, 1000, &quot;,&quot;)) !== FALSE) {
  foreach( $data as $v ) {
     $insertValues=&quot;&#039;&quot;.addslashes(trim($v)).&quot;&#039;&quot;;
  }
  $values=implode(&#039;,&#039;,$insertValues);
  $sql = &quot;INSERT INTO `kmmb_member1` ( $columns ) VALUES ( $values )&quot;;
  mysql_query($sql) or die(&#039;SQL ERROR:&#039;.mysql_error());
}
fclose($handle); 
print &quot;Import done&quot;;
   }
   else
   {
 
      print &quot;&quot;;
      print &quot;Type file name to import:&quot;;
      print &quot;&quot;;
      print &quot;&quot;;
   }</description>
		<content:encoded><![CDATA[<p>the code is like this,..</p>
<p>if(isset($_POST['submit']))<br />
   {<br />
$columns = &#8220;no_ahli, no_pin, nama, no_ic_baru, no_ic_lama&#8221;;<br />
$handle = fopen(&#8220;filename.csv&#8221;, &#8220;r&#8221;);<br />
while (($data = fgetcsv($handle, 1000, &#8220;,&#8221;)) !== FALSE) {<br />
  foreach( $data as $v ) {<br />
     $insertValues=&#8221;&#8216;&#8221;.addslashes(trim($v)).&#8221;&#8216;&#8221;;<br />
  }<br />
  $values=implode(&#8216;,&#8217;,$insertValues);<br />
  $sql = &#8220;INSERT INTO `kmmb_member1` ( $columns ) VALUES ( $values )&#8221;;<br />
  mysql_query($sql) or die(&#8216;SQL ERROR:&#8217;.mysql_error());<br />
}<br />
fclose($handle);<br />
print &#8220;Import done&#8221;;<br />
   }<br />
   else<br />
   {</p>
<p>      print &#8220;&#8221;;<br />
      print &#8220;Type file name to import:&#8221;;<br />
      print &#8220;&#8221;;<br />
      print &#8220;&#8221;;<br />
   }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gabi Solomon</title>
		<link>http://www.gsdesign.ro/blog/parsing-csv-file-with-php-and-inserting-into-mysql-database/comment-page-1/#comment-7522</link>
		<dc:creator>Gabi Solomon</dc:creator>
		<pubDate>Thu, 14 May 2009 07:18:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.gsdesign.ro/blog/?p=59#comment-7522</guid>
		<description>it means you have a different number of columns in mysql then in your CSV file</description>
		<content:encoded><![CDATA[<p>it means you have a different number of columns in mysql then in your CSV file</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: yanti</title>
		<link>http://www.gsdesign.ro/blog/parsing-csv-file-with-php-and-inserting-into-mysql-database/comment-page-1/#comment-7521</link>
		<dc:creator>yanti</dc:creator>
		<pubDate>Thu, 14 May 2009 06:45:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.gsdesign.ro/blog/?p=59#comment-7521</guid>
		<description>hi,...i had tried use ur code but it gives me an error saying that SQL error: Column count doesnt match with values count in 1 row...
wht does it mean?</description>
		<content:encoded><![CDATA[<p>hi,&#8230;i had tried use ur code but it gives me an error saying that SQL error: Column count doesnt match with values count in 1 row&#8230;<br />
wht does it mean?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gabi Solomon</title>
		<link>http://www.gsdesign.ro/blog/parsing-csv-file-with-php-and-inserting-into-mysql-database/comment-page-1/#comment-7479</link>
		<dc:creator>Gabi Solomon</dc:creator>
		<pubDate>Wed, 15 Apr 2009 20:15:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.gsdesign.ro/blog/?p=59#comment-7479</guid>
		<description>@Tom
I agree with you that is a much more faster option.
This was just to show how you could do it with php.</description>
		<content:encoded><![CDATA[<p>@Tom<br />
I agree with you that is a much more faster option.<br />
This was just to show how you could do it with php.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Schäfer</title>
		<link>http://www.gsdesign.ro/blog/parsing-csv-file-with-php-and-inserting-into-mysql-database/comment-page-1/#comment-7478</link>
		<dc:creator>Tom Schäfer</dc:creator>
		<pubDate>Wed, 15 Apr 2009 19:44:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.gsdesign.ro/blog/?p=59#comment-7478</guid>
		<description>Your way of importing csv data is too slow.
Ever thought of using LOAD DATA INFILE ? 
The major work is to clean corrupted file data. But these operations have their focus on normalizing the columns in the import files. (Unicode checks, Linebreaks converting, identifying separators and delimiters)

When these works are done, an import of big files is very fast.
1 million lines in less then 60 seconds. The same row by row lasts 10 minutes and more.
Cheers.</description>
		<content:encoded><![CDATA[<p>Your way of importing csv data is too slow.<br />
Ever thought of using LOAD DATA INFILE ?<br />
The major work is to clean corrupted file data. But these operations have their focus on normalizing the columns in the import files. (Unicode checks, Linebreaks converting, identifying separators and delimiters)</p>
<p>When these works are done, an import of big files is very fast.<br />
1 million lines in less then 60 seconds. The same row by row lasts 10 minutes and more.<br />
Cheers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gabi Solomon</title>
		<link>http://www.gsdesign.ro/blog/parsing-csv-file-with-php-and-inserting-into-mysql-database/comment-page-1/#comment-7196</link>
		<dc:creator>Gabi Solomon</dc:creator>
		<pubDate>Tue, 25 Nov 2008 15:45:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.gsdesign.ro/blog/?p=59#comment-7196</guid>
		<description>thanks for the error spoting :)
fixed</description>
		<content:encoded><![CDATA[<p>thanks for the error spoting <img src='http://www.gsdesign.ro/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
fixed</p>
]]></content:encoded>
	</item>
</channel>
</rss>

