<?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>Chris Cook .me &#187; mysql</title>
	<atom:link href="http://chriscook.me/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://chriscook.me</link>
	<description>Homepage</description>
	<lastBuildDate>Sat, 23 Oct 2010 22:53:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Use PHP to Backup your MySQL Database</title>
		<link>http://chriscook.me/web-development/backup-mysql-database-php/</link>
		<comments>http://chriscook.me/web-development/backup-mysql-database-php/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 20:00:13 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[automated backup]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[chris cook]]></category>
		<category><![CDATA[cron job]]></category>
		<category><![CDATA[databsae]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://chriscook.me/?p=104</guid>
		<description><![CDATA[If you don&#8217;t backup your databases regularly, shame on you. You should! The code below will allow you to generate a backup as often as you&#8217;d like. It only makes sense to run this via a CRON job. This is pretty easy to do and you can set the frequency as you wish. The backup [...]]]></description>
			<content:encoded><![CDATA[<p>If you don&#8217;t backup your databases regularly, shame on you.  You should!</p>
<p>The code below will allow you to generate a backup as often as you&#8217;d like.  It only makes sense to run this via a CRON job.  This is pretty easy to do and you can set the frequency as you wish.</p>
<p>The backup files will be stored in the same directory as this script.  </p>
<p><em><strong>Please leave feedback and let me know if this works for you!</strong></em></p>
<p>Here&#8217;s the PHP code:<br />
{code type=php}<br />
backup_tables(&#8216;localhost&#8217;,'username&#8217;,'password&#8217;,'blog&#8217;);</p>
<p>// Backup the entire database or just a specific table.<br />
function backup_tables($host,$user,$pass,$name,$tables = &#8216;*&#8217;)<br />
{</p>
<p>	$link = mysql_connect($host,$user,$pass);<br />
	mysql_select_db($name,$link);</p>
<p>	//get all of the tables<br />
	if($tables == &#8216;*&#8217;)<br />
	{<br />
		$tables = array();<br />
		$result = mysql_query(&#8216;SHOW TABLES&#8217;);<br />
		while($row = mysql_fetch_row($result))<br />
		{<br />
			$tables[] = $row[0];<br />
		}<br />
	}<br />
	else<br />
	{<br />
		$tables = is_array($tables) ? $tables : explode(&#8216;,&#8217;,$tables);<br />
	}</p>
<p>	//This method is completed for each table<br />
	foreach($tables as $table)<br />
	{<br />
		$result = mysql_query(&#8216;SELECT * FROM &#8216;.$table);<br />
		$num_fields = mysql_num_fields($result);</p>
<p>		$return.= &#8216;DROP TABLE &#8216;.$table.&#8217;;';<br />
		$row2 = mysql_fetch_row(mysql_query(&#8216;SHOW CREATE TABLE &#8216;.$table));<br />
		$return.= &#8220;\n\n&#8221;.$row2[1].&#8221;;\n\n&#8221;;</p>
<p>		for ($i = 0; $i < $num_fields; $i++)<br />
		{<br />
			while($row = mysql_fetch_row($result))<br />
			{<br />
				$return.= &#8216;INSERT INTO &#8216;.$table.&#8217; VALUES(&#8216;;<br />
				for($j=0; $j<$num_fields; $j++)<br />
				{<br />
					$row[$j] = addslashes($row[$j]);<br />
					$row[$j] = ereg_replace(&#8220;\n&#8221;,&#8221;\\n&#8221;,$row[$j]);<br />
					if (isset($row[$j])) { $return.= &#8216;&#8221;&#8216;.$row[$j].&#8217;&#8221;&#8216; ; } else { $return.= &#8216;&#8221;"&#8216;; }<br />
					if ($j<($num_fields-1)) { $return.= &#8216;,&#8217;; }<br />
				}<br />
				$return.= &#8220;);\n&#8221;;<br />
			}<br />
		}<br />
		$return.=&#8221;\n\n\n&#8221;;<br />
	}</p>
<p>	//Now, we&#8217;ll save the file<br />
	$handle = fopen(&#8216;backup-&#8217;.time().&#8217;-&#8217;.(md5(implode(&#8216;,&#8217;,$tables))).&#8217;.sql&#8217;,'w+&#8217;);<br />
	fwrite($handle,$return);<br />
	fclose($handle);<br />
}<br />
{/code}</p>
]]></content:encoded>
			<wfw:commentRss>http://chriscook.me/web-development/backup-mysql-database-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Return Random Record via MySQL</title>
		<link>http://chriscook.me/web-development/return-random-record-via-mysql/</link>
		<comments>http://chriscook.me/web-development/return-random-record-via-mysql/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 20:01:54 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[chris cook]]></category>
		<category><![CDATA[featured]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[RAND]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://chriscook.me/?p=96</guid>
		<description><![CDATA[There are many practical and frequently used methods requiring a random record to be called. For instance, when you see &#8220;featured profiles&#8221; or random customer comments on a website, they are most likely using a function to call a random record. In addition, this function can allow you to display data in a random order. [...]]]></description>
			<content:encoded><![CDATA[<p>There are many practical and frequently used methods requiring a random record to be called.  For instance, when you see &#8220;featured profiles&#8221; or random customer comments on a website, they are most likely using a function to call a random record.</p>
<p>In addition, this function can allow you to display data in a random order.</p>
<p>Here&#8217;s how it works:<br />
{code type=mysql}<br />
SELECT field_1, field_2, field_3, field_4<br />
FROM table_name<br />
WHERE parameter = 1<br />
ORDER BY RAND()<br />
LIMIT 1<br />
{/code}</p>
<p>It&#8217;s that simple.  Please post your feedback!</p>
]]></content:encoded>
			<wfw:commentRss>http://chriscook.me/web-development/return-random-record-via-mysql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP/AJAX: Call PHP function by clicking a link</title>
		<link>http://chriscook.me/web-development/phpajax-execute-php-function-by-clicking-a-link/</link>
		<comments>http://chriscook.me/web-development/phpajax-execute-php-function-by-clicking-a-link/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 22:14:24 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[chris cook]]></category>
		<category><![CDATA[chriscook.me]]></category>
		<category><![CDATA[execute php function in ajax request]]></category>
		<category><![CDATA[link]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://chriscook.me/?p=46</guid>
		<description><![CDATA[This tutorial demonstrates how to execute an external PHP function by clicking a simple link within HTML.  The method uses AJAX so that the page doesn&#8217;t refresh and doesn&#8217;t require a form submission. 1) Paste the following code into a .js file.  For demonstration purposes, we have named it &#8220;ajax_click.js&#8221;. {code type=html} /* * ajax_click.js [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial demonstrates how to execute an external PHP function by clicking a simple link within HTML.  The method uses AJAX so that the page doesn&#8217;t refresh and doesn&#8217;t require a form submission.</p>
<p><strong>1)</strong> Paste the following code into a .js file.  For demonstration purposes, we have named it &#8220;ajax_click.js&#8221;.</p>
<p>{code type=html}</p>
<p>/*<br />
* ajax_click.js<br />
*  chriscook.me<br />
*/</p>
<p>function loadurl(dest) {</p>
<p>try {<br />
// Moz supports XMLHttpRequest. IE uses ActiveX.<br />
// browser detction is bad. object detection works for any browser<br />
xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />
} catch (e) {<br />
// browser doesn&#8217;t support ajax. handle however you want<br />
}</p>
<p>// the xmlhttp object triggers an event everytime the status changes<br />
// triggered() function handles the events<br />
xmlhttp.onreadystatechange = triggered;</p>
<p>// open takes in the HTTP method and url.<br />
xmlhttp.open(&#8220;GET&#8221;, dest);</p>
<p>// send the request. if this is a POST request we would have<br />
// sent post variables: send(&#8220;name=aleem gender=male)<br />
// Moz is fine with just send(); but<br />
// IE expects a value here, hence we do send(null);<br />
xmlhttp.send(&#8220;null&#8221;);<br />
}</p>
<p>function triggered() {<br />
if ((xmlhttp.readyState == 4) (xmlhttp.status == 200)) {</p>
<p>document.getElementById(&#8220;ajaxlink&#8221;).innerHTML = xmlhttp.responseText;<br />
}<br />
}<br />
{/code}</p>
<p><strong>2)</strong> Next, add the following code in the  section of your HTML file.</p>
<p>{code type=html}&lt;script src=&#8221;ajax_link.js&#8221; type=&#8221;text/javascript&#8221;&gt;&lt;/script&gt;{/code}</p>
<p><strong>3)</strong> The following code should be placed in the HTML body of a PHP file.</p>
<p>{code type=html}</p>
<p>&lt;div id=&#8221;ajaxlink&#8221; onclick=&#8221;loadurl(&#8216;ajax_function.php&#8217;)&#8221;&gt;Click Here&lt;/div&gt;</p>
<p>{/code}</p>
<p>Replace &#8216;ajax_function.php&#8217; with the correct file you want to execute.  For instance, if you want your users to &#8220;click here&#8221; in order to send themselves a copy of their monthly invoice, you&#8217;d code the mailer function in &#8216;ajax_function.php&#8217;.</p>
<p><strong>4) </strong>That&#8217;s it.  Ensure that you keep the &lt;div&gt; id  as &#8220;ajaxlink&#8221;.  Once you click the link, it will disappear (until the page is refreshed).</p>
<p><strong>Feedback:</strong> I&#8217;d love to hear how you&#8217;ve used this tool.  Please feel free to post a comment on my blog.</p>
<p><strong>Disclaimer: </strong> This tutorial is provided to demonstrate how to perform the function.  Please ensure that you review the code and add security measures before using this in a production environment.</p>
]]></content:encoded>
			<wfw:commentRss>http://chriscook.me/web-development/phpajax-execute-php-function-by-clicking-a-link/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

