<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.5" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: PHP: Export Query Results to a CSV File</title>
	<link>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/</link>
	<description>Utterly random, incoherent and disjointed rants and ramblings...</description>
	<pubDate>Thu, 08 Jan 2009 02:49:06 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.5</generator>

	<item>
		<title>by: Dave</title>
		<link>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/#comment-9750</link>
		<pubDate>Thu, 31 Jul 2008 15:48:01 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/#comment-9750</guid>
					<description>Thanks.  I tried several scripts to do this but yours was simplest and most effective.</description>
		<content:encoded><![CDATA[<p>Thanks.  I tried several scripts to do this but yours was simplest and most effective.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Adam</title>
		<link>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/#comment-9142</link>
		<pubDate>Tue, 27 May 2008 14:20:14 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/#comment-9142</guid>
					<description>Thanks for the great info!  This really helped me a lot.</description>
		<content:encoded><![CDATA[<p>Thanks for the great info!  This really helped me a lot.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Chris</title>
		<link>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/#comment-6878</link>
		<pubDate>Thu, 08 Nov 2007 09:36:14 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/#comment-6878</guid>
					<description>Thanks Luke, that worked great and really helps with my learning. I will be sure to explore more of your PHP/MySQL posts.
Chris</description>
		<content:encoded><![CDATA[<p>Thanks Luke, that worked great and really helps with my learning. I will be sure to explore more of your PHP/MySQL posts.<br />
Chris
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Luke Maciak</title>
		<link>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/#comment-6868</link>
		<pubDate>Wed, 07 Nov 2007 17:54:23 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/#comment-6868</guid>
					<description>You want to run your query in the usual way and then you pass the result to the function:

&lt;pre lang="php"&gt;&lt;?php
// lets say my function is in a file cvs_export.php
require("cvs_export.php")

// connect to the db
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password');
mysql_select_db('my_database');

// run the query
$result = mysql_query("SELECT * FROM my_table");

// pass the result to my function:
csv_from_mysql_resource($result);
?&gt;&lt;/pre&gt;

As for the error, it almost looks like something you would get with PHP4 which has no passing by reference. You can easily rewrite the foreach loop  like this:

&lt;pre lang="php"&gt;foreach ($row as $key =&gt; $value)
{
        $row[$key] = str_replace("\r\n", "", $value);
        $row[$key] = "\"" . $value . "\"";
}&lt;/pre&gt;
 
This removes the pass by reference syntax that seems to be causing the error. Just make sure you don't have any white spaces before or after the PHP tags and you should be fine. :)</description>
		<content:encoded><![CDATA[<p>You want to run your query in the usual way and then you pass the result to the function:</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #808080; font-style: italic;">// lets say my function is in a file cvs_export.php</span>
<span style="color: #b1b100;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;cvs_export.php&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// connect to the db</span>
<span style="color: #0000ff;">$link</span> = <span style="color: #000066;">mysql_connect</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'mysql_host'</span>, <span style="color: #ff0000;">'mysql_user'</span>, <span style="color: #ff0000;">'mysql_password'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">mysql_select_db</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'my_database'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// run the query</span>
<span style="color: #0000ff;">$result</span> = <span style="color: #000066;">mysql_query</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;SELECT * FROM my_table&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// pass the result to my function:</span>
csv_from_mysql_resource<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>As for the error, it almost looks like something you would get with PHP4 which has no passing by reference. You can easily rewrite the foreach loop  like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$row</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$key</span> =&gt; <span style="color: #0000ff;">$value</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$row</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$key</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #000066;">str_replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, <span style="color: #ff0000;">&quot;&quot;</span>, <span style="color: #0000ff;">$value</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$row</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$key</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> . <span style="color: #0000ff;">$value</span> . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This removes the pass by reference syntax that seems to be causing the error. Just make sure you don&#8217;t have any white spaces before or after the PHP tags and you should be fine. <img src="http://www.terminally-incoherent.com/blog/wp-includes/images/smilies/icon_smile.gif" alt=")" class="wp-smiley" />
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Chris</title>
		<link>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/#comment-6867</link>
		<pubDate>Wed, 07 Nov 2007 17:34:40 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/#comment-6867</guid>
					<description>I'm a PHP newbie and don't understand how you get this function to execute and pass it a query it'll output in CSV format.

I either get the error message "Parse error: parse error, unexpected '&#38;', expecting T_VARIABLE or '$'" in the line where "foreach ($row as &#38;$value)
" appears - I'm running PHP5 - or else a blank white page. Any chance you can expand on implementing this function?</description>
		<content:encoded><![CDATA[<p>I&#8217;m a PHP newbie and don&#8217;t understand how you get this function to execute and pass it a query it&#8217;ll output in CSV format.</p>
<p>I either get the error message &#8220;Parse error: parse error, unexpected &#8216;&amp;&#8217;, expecting T_VARIABLE or &#8216;$&#8217;&#8221; in the line where &#8220;foreach ($row as &amp;$value)<br />
&#8221; appears - I&#8217;m running PHP5 - or else a blank white page. Any chance you can expand on implementing this function?
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Luke Maciak</title>
		<link>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/#comment-6769</link>
		<pubDate>Tue, 30 Oct 2007 16:02:14 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/#comment-6769</guid>
					<description>Wordpress strips HTML tags by default. If you use the code button you can override that behavior. Either that, or use HTML entities &#38;lt; and &#38;gt; for &#60; and &#62;

Thanks for the tip though  :)</description>
		<content:encoded><![CDATA[<p>Wordpress strips HTML tags by default. If you use the code button you can override that behavior. Either that, or use HTML entities &amp;lt; and &amp;gt; for &lt; and &gt;</p>
<p>Thanks for the tip though  <img src="http://www.terminally-incoherent.com/blog/wp-includes/images/smilies/icon_smile.gif" alt=")" class="wp-smiley" />
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Joe</title>
		<link>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/#comment-6767</link>
		<pubDate>Tue, 30 Oct 2007 15:25:32 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/#comment-6767</guid>
					<description>I meant: ''</description>
		<content:encoded><![CDATA[<p>I meant: &#8216;&#8217;
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Joe</title>
		<link>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/#comment-6766</link>
		<pubDate>Tue, 30 Oct 2007 15:23:55 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/#comment-6766</guid>
					<description>replace it with an HTML Line break: 

the  's got eaten out of my last comment.</description>
		<content:encoded><![CDATA[<p>replace it with an HTML Line break: </p>
<p>the  &#8217;s got eaten out of my last comment.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Joe</title>
		<link>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/#comment-6765</link>
		<pubDate>Tue, 30 Oct 2007 15:22:15 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/#comment-6765</guid>
					<description>Instead of removing the '/r/n' you should consider replacing it with '' to preserve the line break position without causing havoc in your CSV.  

You can always convert back from '' to '\r\n' if you need to later but you cannot add newlines back in if you removed them...</description>
		<content:encoded><![CDATA[<p>Instead of removing the &#8216;/r/n&#8217; you should consider replacing it with &#8216;&#8217; to preserve the line break position without causing havoc in your CSV.  </p>
<p>You can always convert back from &#8216;&#8217; to &#8216;\r\n&#8217; if you need to later but you cannot add newlines back in if you removed them&#8230;
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Barbara F</title>
		<link>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/#comment-6526</link>
		<pubDate>Thu, 11 Oct 2007 17:21:30 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2007/07/05/php-export-query-results-to-a-csv-file/#comment-6526</guid>
					<description>This is EXACTLY what I was looking for - and so simple to use.

Thank you!!!!!</description>
		<content:encoded><![CDATA[<p>This is EXACTLY what I was looking for - and so simple to use.</p>
<p>Thank you!!!!!
</p>
]]></content:encoded>
				</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.625 seconds -->
