<?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>NMP Blog &#187; MySQL</title>
	<atom:link href="http://nmpconsulting.com/blog/category/programming/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://nmpconsulting.com/blog</link>
	<description>Solutions to your technology issues.</description>
	<lastBuildDate>Wed, 04 Jan 2012 19:36:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>PHP MySQL Dump or Export</title>
		<link>http://nmpconsulting.com/blog/2009/11/03/php-mysql-dump-or-export/</link>
		<comments>http://nmpconsulting.com/blog/2009/11/03/php-mysql-dump-or-export/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 16:27:04 +0000</pubDate>
		<dc:creator>Chris Sunami</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://nmpconsulting.com/blog/?p=106</guid>
		<description><![CDATA[You have php access to a MySQL --or other SQL variant-- server.  You don't have phpMyAdmin, you don't have command line access.  You would like to export some of the data, but not necessarily all of it, because perhaps the full database is too large.]]></description>
			<content:encoded><![CDATA[<p>I searched all over for this information, and never found it in a easy-to-use format:</p>
<p>Here&#8217;s the situation.  You have php access to a MySQL &#8211;or other SQL variant&#8211; server.  You don&#8217;t have phpMyAdmin, you don&#8217;t have command line access.  You would like to export some of the data, but not necessarily all of it, because perhaps the full database is too large.</p>
<p>The SQL command that makes this simple is: &#8220;SHOW CREATE TABLE <em>table_name</em>&#8221;</p>
<p>Use that as your query, and the output will be the complete SQL command for creating a table.  The only other thing you need is to turn the data output into insert statements, which is trivial.</p>
<p>Here&#8217;s a complete script &#8211;notice how short it is:<br />
<code><br />
mysql_connect($sql_host, $sql_user, $sql_password) or exit(mysql_error());<br />
mysql_select_db($sql_db) or exit(mysql_error());<br />
$result=mysql_query("SHOW TABLES");<br />
echo mysql_error();<br />
while ($row=mysql_fetch_array($result)){<br />
  $tableName=$row[0];<br />
  $query2="SHOW CREATE TABLE $tableName";<br />
  $result2 = mysql_query($query2);<br />
  $row2=mysql_fetch_array($result2);<br />
  echo "$row2[1];\n";<br />
  $query3      = "SELECT * FROM $tableName";<br />
  $result3 = mysql_query($query3);<br />
  while ($row3=mysql_fetch_array($result3)){<br />
    $header_array=array();<br />
    $line_array=array();<br />
    foreach ($row3 as $key=&gt;$value){<br />
      if (!is_numeric($key)){<br />
        $header_array[]="$key";<br />
        $line_array[]="'$value'";<br />
      }<br />
    }<br />
    echo "INSERT INTO $tableName (".implode(", ", $header_array).") VALUES (".implode(", ",$line_array).");\n";<br />
    }<br />
  }<br />
}<br />
mysql_close();<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://nmpconsulting.com/blog/2009/11/03/php-mysql-dump-or-export/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

