<?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, 31 Jul 2010 22:52:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</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:</p>
<pre class="php">
backup_tables<span class="phpOperator">(</span><span class="phpString">'localhost'</span>,<span class="phpString">'username'</span>,<span class="phpString">'password'</span>,<span class="phpString">'blog'</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpComment">// Backup the entire database or just a specific table.
</span><span class="phpFunctionKeyword">function</span> backup_tables<span class="phpOperator">(</span>$host,$user,$pass,$name,$tables <span class="phpOperator">=</span> <span class="phpString">'*'</span><span class="phpOperator">)</span>
<span class="phpOperator">{</span>
	$link <span class="phpOperator">=</span> <span class="phpFunction">mysql_connect</span><span class="phpOperator">(</span>$host,$user,$pass<span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpFunction">mysql_select_db</span><span class="phpOperator">(</span>$name,$link<span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpComment">//get all of the tables
</span><span class="phpKeyword">	if<span class="phpOperator">(</span></span>$tables <span class="phpOperator"><span class="phpOperator">=</span>=</span> <span class="phpString">'*'</span><span class="phpOperator">)</span>
	<span class="phpOperator">{</span>
		$tables <span class="phpOperator">=</span> <span class="phpFunction">array</span><span class="phpOperator">(</span><span class="phpOperator">)</span><span class="phpText">;</span>
		$result <span class="phpOperator">=</span> <span class="phpFunction">mysql_query</span><span class="phpOperator">(</span><span class="phpString">'SHOW TABLES'</span><span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpKeyword">	while<span class="phpOperator">(</span></span>$row <span class="phpOperator">=</span> <span class="phpFunction">mysql_fetch_row</span><span class="phpOperator">(</span>$result<span class="phpOperator">)</span><span class="phpOperator">)</span>
		<span class="phpOperator">{</span>
			$tables<span class="phpOperator">[</span><span class="phpOperator">]</span> <span class="phpOperator">=</span> $row<span class="phpOperator">[</span><span class="phpNumber">0</span><span class="phpOperator">]</span><span class="phpText">;</span>
		<span class="phpOperator">}</span>
	<span class="phpOperator">}</span>
<span class="phpKeyword">	else
</span>
	<span class="phpOperator">{</span>
		$tables <span class="phpOperator">=</span> is_<span class="phpFunction">array</span><span class="phpOperator">(</span>$tables<span class="phpOperator">)</span> <span class="phpOperator">?</span> $tables <span class="phpOperator">:</span> <span class="phpFunction">explode</span><span class="phpOperator">(</span><span class="phpString">','</span>,$tables<span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpOperator">}</span>
	<span class="phpComment">//This method is completed<span class="phpKeyword"> for </span>each table
</span><span class="phpKeyword">	foreach<span class="phpOperator">(</span></span>$tables<span class="phpKeyword"> as </span>$table<span class="phpOperator">)</span>
	<span class="phpOperator">{</span>
		$result <span class="phpOperator">=</span> <span class="phpFunction">mysql_query</span><span class="phpOperator">(</span><span class="phpString">'SELECT * FROM '</span>.$table<span class="phpOperator">)</span><span class="phpText">;</span>
		$num_fields <span class="phpOperator">=</span> <span class="phpFunction">mysql_num_fields</span><span class="phpOperator">(</span>$result<span class="phpOperator">)</span><span class="phpText">;</span>
		$return<span class="phpOperator">.=</span> <span class="phpString">'DROP TABLE '</span>.$table.<span class="phpString">'<span class="phpText">;</span>'</span><span class="phpText">;</span>
		$row2 <span class="phpOperator">=</span> <span class="phpFunction">mysql_fetch_row</span><span class="phpOperator">(</span><span class="phpFunction">mysql_query</span><span class="phpOperator">(</span><span class="phpString">'SHOW CREATE TABLE '</span>.$table<span class="phpOperator">)</span><span class="phpOperator">)</span><span class="phpText">;</span>
		$return<span class="phpOperator">.=</span> <span class="phpString">"\n\n"</span>.$row2<span class="phpOperator">[</span><span class="phpNumber">1</span><span class="phpOperator">]</span>.<span class="phpString">"<span class="phpText">;</span>\n\n"</span><span class="phpText">;</span>
	<span class="phpKeyword">	for </span><span class="phpOperator">(</span>$i <span class="phpOperator">=</span> 0; $i <span class="phpOperator">&lt;</span> $num_fields<span class="phpText">;</span> $i<span class="phpOperator"><span class="phpOperator">+</span><span class="phpOperator">+</span></span><span class="phpOperator">)</span>
		<span class="phpOperator">{</span>
		<span class="phpKeyword">	while<span class="phpOperator">(</span></span>$row <span class="phpOperator">=</span> <span class="phpFunction">mysql_fetch_row</span><span class="phpOperator">(</span>$result<span class="phpOperator">)</span><span class="phpOperator">)</span>
			<span class="phpOperator">{</span>
				$return<span class="phpOperator">.=</span> <span class="phpString">'INSERT INTO '</span>.$table.<span class="phpString">' VALUES<span class="phpOperator">(</span>'</span><span class="phpText">;</span>
			<span class="phpKeyword">	for<span class="phpOperator">(</span></span>$j<span class="phpOperator">=</span>0; $j<span class="phpOperator">&lt;</span>$num_fields<span class="phpText">;</span> $j<span class="phpOperator"><span class="phpOperator">+</span><span class="phpOperator">+</span></span><span class="phpOperator">)</span>
				<span class="phpOperator">{</span>
					$row<span class="phpOperator">[</span>$j<span class="phpOperator">]</span> <span class="phpOperator">=</span> <span class="phpFunction">addslashes</span><span class="phpOperator">(</span>$row<span class="phpOperator">[</span>$j<span class="phpOperator">]</span><span class="phpOperator">)</span><span class="phpText">;</span>
					$row<span class="phpOperator">[</span>$j<span class="phpOperator">]</span> <span class="phpOperator">=</span> <span class="phpFunction">ereg_replace</span><span class="phpOperator">(</span><span class="phpString">"\n"</span>,<span class="phpString">"\\n"</span>,$row<span class="phpOperator">[</span>$j<span class="phpOperator">]</span><span class="phpOperator">)</span><span class="phpText">;</span>
				<span class="phpKeyword">	if </span><span class="phpOperator">(</span><span class="phpFunction">isset</span><span class="phpOperator">(</span>$row<span class="phpOperator">[</span>$j<span class="phpOperator">]</span><span class="phpOperator">)</span><span class="phpOperator">)</span> <span class="phpOperator">{</span> $return<span class="phpOperator">.=</span> <span class="phpString">'<span class="phpString">"'</span>.$row<span class="phpOperator">[</span>$j<span class="phpOperator">]</span>.<span class="phpString">'"' ; } else { $return.= '""</span>'</span><span class="phpText">;</span> <span class="phpOperator">}</span>
				<span class="phpKeyword">	if </span><span class="phpOperator">(</span>$j<span class="phpOperator">&lt;</span><span class="phpOperator">(</span>$num_fields-<span class="phpNumber">1</span><span class="phpOperator">)</span><span class="phpOperator">)</span> <span class="phpOperator">{</span> $return<span class="phpOperator">.=</span> <span class="phpString">','</span><span class="phpText">;</span> <span class="phpOperator">}</span>
				<span class="phpOperator">}</span>
				$return<span class="phpOperator">.=</span> <span class="phpString">"<span class="phpOperator">)</span><span class="phpText">;</span>\n"</span><span class="phpText">;</span>
			<span class="phpOperator">}</span>
		<span class="phpOperator">}</span>
		$return<span class="phpOperator">.=</span><span class="phpString">"\n\n\n"</span><span class="phpText">;</span>
	<span class="phpOperator">}</span>
	<span class="phpComment">//Now, we<span class="phpString">'ll save the <span class="phpFunction">file</span>
</span>	$handle <span class="phpOperator">=</span> <span class="phpFunction">fopen</span><span class="phpOperator">(</span>'</span>backup-<span class="phpString">'<span class="phpOperator">.</span><span class="phpFunction">time</span><span class="phpOperator">(</span><span class="phpOperator">)</span>.'</span>-<span class="phpString">'<span class="phpOperator">.</span><span class="phpOperator">(</span><span class="phpFunction">md5</span><span class="phpOperator">(</span><span class="phpFunction">implode</span><span class="phpOperator">(</span>'</span>,<span class="phpString">',$tables<span class="phpOperator">)</span><span class="phpOperator">)</span><span class="phpOperator">)</span>.'</span>.sql<span class="phpString">','</span>w<span class="phpOperator">+</span>&#039;<span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpFunction">fwrite</span><span class="phpOperator">(</span>$handle,$return<span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpFunction">fclose</span><span class="phpOperator">(</span>$handle<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpOperator">}</span>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://chriscook.me/web-development/backup-mysql-database-php/feed/</wfw:commentRss>
		<slash:comments>2</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:</p>
<pre class="mysql">
SELECT field_1, field_2, field_3, field_4
FROM table_name
WHERE parameter = 1
ORDER BY RAND()
LIMIT 1
</pre>
<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;. /* * ajax_click.js * chriscook.me [...]]]></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>
<pre class="html">
/*
* ajax_click.js
*  chriscook.me
*/
function loadurl(dest) {
try {
// Moz supports XMLHttpRequest. IE uses ActiveX.
// browser detction is bad. object detection works for any browser
xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
} catch (e) {
// browser doesn&#039;t support ajax. handle however you want
}
// the xmlhttp object triggers an event everytime the status changes
// triggered() function handles the events
xmlhttp.onreadystatechange = triggered;
// open takes in the HTTP method and url.
xmlhttp.open(&quot;GET&quot;, dest);
// send the request. if this is a POST request we would have
// sent post variables: send(&quot;name=aleem gender=male)
// Moz is fine with just send(); but
// IE expects a value here, hence we do send(null);
xmlhttp.send(&quot;null&quot;);
}
function triggered() {
if ((xmlhttp.readyState == 4) (xmlhttp.status == 200)) {
document.getElementById(&quot;ajaxlink&quot;).innerHTML = xmlhttp.responseText;
}
}
</pre>
<p><strong>2)</strong> Next, add the following code in the  section of your HTML file.</p>
<pre class="html"><span class="htmlScriptTag">&lt;script src=<span class="htmlAttributeValue">&quot;ajax_link.js&quot;</span> type=<span class="htmlAttributeValue">&quot;text/javascript&quot;</span>&gt;</span><span class="htmlScriptTag">&lt;/script&gt;</span></pre>
<p><strong>3)</strong> The following code should be placed in the HTML body of a PHP file.</p>
<pre class="html">
<span class="htmlOtherTag">&lt;div id=<span class="htmlAttributeValue">&quot;ajaxlink&quot;</span> onclick=<span class="htmlAttributeValue">&quot;loadurl(&#039;ajax_function.php&#039;)&quot;</span>&gt;</span>Click Here<span class="htmlOtherTag">&lt;/div&gt;</span>
</pre>
<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>
