<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Georgi Mitev &#124; Learn Scripts Tutorials &#124; PHP Cron Mysql Flash &#187; export CSV</title>
	<atom:link href="http://georgi-mitev.com/tag/export-csv/feed/" rel="self" type="application/rss+xml" />
	<link>http://georgi-mitev.com</link>
	<description>Georgi Mitev&#039;s blog for scripts tutorials and something else</description>
	<lastBuildDate>Thu, 29 Apr 2010 09:00:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Export / Import Excel file to sql / database using CSV and PHP</title>
		<link>http://georgi-mitev.com/2009/03/09/export-import-excel-file-to-sql-database-using-csv-and-php/</link>
		<comments>http://georgi-mitev.com/2009/03/09/export-import-excel-file-to-sql-database-using-csv-and-php/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 09:40:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[export CSV]]></category>
		<category><![CDATA[import CSV]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://georgi-mitev.com/?p=52</guid>
		<description><![CDATA[Hello everybody,
today i&#8217;ll be talking about how to export / import your Excel file into you your database using the PHP and the CSV file format.
First of all we need an Excel file with our data.
Every row of the excel file should be the same format as the previous row (so that we can make [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Hello everybody</strong>,</p>
<p>today i&#8217;ll be talking about how to export / import your Excel file into you your database using the PHP and the CSV file format.<br />
First of all we need an Excel file with our data.</p>
<p>Every row of the excel file should be the same format as the previous row (so that we can make our life easier), for example if we want to import a excel file with name/url addres corresponding to this name we should have our excel file in a format like this:<br />
<img src="http://georgi-mitev.com/images/posts/3.2009/excel.png" alt="exel file example" /><br />
After u have the file in this excel format u should hit -&gt; File -&gt; Save as -&gt; CSV (Comma Delimited) or more ofter its Called &#8220;Comma Separated Values&#8221;. For the comma separator use &#8220;;&#8221; if it asks you. So after you save the file in the CSV file format you should have something like this after you open the file with notepad etc.:</p>
<p><span id="more-52"></span><br />
<img src="http://georgi-mitev.com/images/posts/3.2009/csv.png" alt="exel file example" /><br />
in this case we use &#8220;;&#8221; for comma separator.</p>
<p>ok lets get now to the Databse &#8211; in my case i will be using mysql, so for the sample excel file above you can use this code to create your database:</p>
<p><span style="color: #888888;">CREATE DATABASE MerchantManager;<br />
USE MerchantManager;<br />
CREATE TABLE IF NOT EXISTS `merchants` (<br />
`MerchantID` int(11) NOT NULL AUTO_INCREMENT,<br />
`MerchantTimestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,<br />
`MerchantName` varchar(256) NOT NULL,<br />
`MerchantURL` varchar(256) NOT NULL,<br />
PRIMARY KEY (`MerchantID`)<br />
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=185 ;</span></p>
<p>after we create the database we are going to write some function in php that can import the data into our Database:</p>
<p>we can create now a new file &#8211; &#8220;import.php&#8221;</p>
<p><span style="color: #888888;">&lt;?<br />
</span></p>
<p><span style="color: #888888;">function AddMerchant($name, $url){<br />
$host = &#8220;localhost&#8221;;<br />
$user = &#8220;root&#8221;;<br />
$pass = &#8220;&#8221;; // use your mysql password<br />
$db_name = &#8220;merchantmanager&#8221;;<br />
$table = &#8220;merchants&#8221;;</span></p>
<p><span style="color: #888888;">mysqli_query(new mysqli($host, $user, $pass, $db_name), &#8220;INSERT INTO &#8220;.$table.&#8221; (`MerchantName`, `MerchantURL`) VALUES (&#8216;$name&#8217;, &#8216;$url&#8217;)&#8221;);</span></p>
<p><span style="color: #888888;">}<br />
?&gt;</span></p>
<p> <img src='http://georgi-mitev.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  okz i hope it&#8217;s all clear for now, so let&#8217;s get to the php now <img src='http://georgi-mitev.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
To export the CSV file into our database we are going to use:</p>
<p>&#8220;file_get_contents&#8221; function &#8211; <a href="http://bg.php.net/file_get_contents">http://bg.php.net/file_get_contents</a><br />
and &#8220;explode&#8221; function &#8211; http://bg.php.net/manual/en/function.explode.php</p>
<p>so lets get started, after the &#8220;AddMerchant()&#8221; function that we wrote above, we will add the following code into the file &#8220;import.php&#8221;</p>
<p><span style="color: #888888;">$file = file_get_contents(&#8220;file.csv&#8221;); //we load the CSV file and put it in a variable</span></p>
<p><span style="color: #888888;">$rows = explode(&#8220;\n&#8221;, $file); //we brake the variable into array, using the new line as a condition</span></p>
<p><span style="color: #888888;">foreach($rows as $row){</span></p>
<p style="padding-left: 30px;"><span style="color: #888888;"> $items = explode(&#8220;;&#8221;, $row); //we brake it again into another array using the &#8220;;&#8221; sign in our case<br />
AddMerchant($items[0],$items[1]);// we get the first and the second value of the array (name and url)<br />
echo &#8220;success&#8221;;</span></p>
<p><span style="color: #888888;">}</span></p>
<p><span style="color: #888888;">?&gt;</span></p>
<p>Can you believe it&#8217;s that simple <img src='http://georgi-mitev.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  3 lines of code and we managed to put the information from the excel file into the database.</p>
<p>if you want to get all the source code from this simple tutorial<br />
download it here: <a href="http://georgi-mitev.com/files/tutorials/export_csv_to_mysql.rar">export_csv_to_mysql.rar</a></p>
]]></content:encoded>
			<wfw:commentRss>http://georgi-mitev.com/2009/03/09/export-import-excel-file-to-sql-database-using-csv-and-php/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
