<?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>PHP Reference Book Blog &#187; MySQL</title>
	<atom:link href="http://www.phpreferencebook.com/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phpreferencebook.com</link>
	<description>PHP Reference: Beginner to Intermediate PHP5</description>
	<lastBuildDate>Thu, 29 Sep 2011 03:50:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Search Speed: Isset vs In_array vs MySQL Query</title>
		<link>http://www.phpreferencebook.com/tips/isset-in_array-mysql/</link>
		<comments>http://www.phpreferencebook.com/tips/isset-in_array-mysql/#comments</comments>
		<pubDate>Thu, 18 Aug 2011 03:07:33 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[in_array]]></category>
		<category><![CDATA[isset]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=445</guid>
		<description><![CDATA[As databases and visitors grow, sometimes &#8216;old code&#8217; doesn&#8217;t work like it use to: it takes too long or times out for exceeding the maximum time allowed by your server for PHP execution. On a MySQL table with 30,000 rows (not too many), the query was checking one column for &#60;3 and then matching a [...]]]></description>
			<content:encoded><![CDATA[<p>As databases and visitors grow, sometimes &#8216;old code&#8217; doesn&#8217;t work like it use to: it takes too long or times out for exceeding the maximum time allowed by your server for PHP execution. On a MySQL table with 30,000 rows (not too many), the query was checking one column for &lt;3 and then matching a long list (10,000) of values in a second column using a long WHERE column2=&#8217;value&#8217; clause. Here is an example of the table:</p>
<pre>
|--column1--|--column2--|
|     0     |    1234   |
|     3     |    5678   |
-------------------------
</pre>
<p>A MySQL query took a while, but didn&#8217;t time out when the list of WHERE column2=&#8217;value&#8217; was less than 7000, but it was just overwhelmed at 10,000. Okay, fine, time to go to phase two: <strong>in_array()</strong>.<br />
There was an existing array of values for column2, so I could simply pull a list of &lt;3 from MySQL (quick) and then loop through (foreach) each of the results and check whether they are <strong>in_array()</strong> for the original source. If they were, just save them to a brand new array that I&#8217;ll use to work further through the problem. For clarification, that means that the system should go through 10,000+ iterations and check if each value is <strong>in_array()</strong>. If you had not already guessed, this was disastrous. <strong>In_Array()</strong> is very inefficient, and if you are dealing with an array that is more than 100 or so values, you&#8217;ll quickly learn this fact. So what to do? Use <strong>isset()</strong>.<br />
<strong>Isset()</strong>, as a function, is incredibly fast. It only checks whether something exists, and is a great way to check for whether something exists in an array. However, the trick is using <strong>isset()</strong> will look at the KEYS, while in_array() will look at the VALUES. Is that problem? Not if you build the original array as <strong>[value]=&gt;value</strong> (set the KEY and VALUE the same). Now, as long as your values are valid keys and unique, this won&#8217;t be a problem. Then, you can use a foreach loop to check whether it is found in the original array. Here is some sample code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$array_source</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'abc'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'abc'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'def'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'def'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'ghi'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'ghi'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$array_results</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'xyz'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'ghi'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array_results</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array_source</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//fast</span>
    <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$v</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//if found, save it to a fresh array</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>What was the final result? The query, run 6 different times for 6 different data sets, went from a total execution time exceeding 2 minutes (MySQL), to not working at all (in_array), to <strong>0.18 seconds</strong>. Yes, less than one second! That is the power for checking whether a value is set when comparing arrays. <strong>Isset()</strong> is even faster by a notable amount when compared to <strong>array_diff()</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/tips/isset-in_array-mysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MySQL Update Multiple Columns Query</title>
		<link>http://www.phpreferencebook.com/tips/mysql-update-multiple-columns-query/</link>
		<comments>http://www.phpreferencebook.com/tips/mysql-update-multiple-columns-query/#comments</comments>
		<pubDate>Fri, 17 Jun 2011 21:19:57 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=432</guid>
		<description><![CDATA[While the book only includes a single example on page 117 for the MySQL UPDATE query, it should have also included an example of a multiple column UPDATE. Here is an example: UPDATE table SET column1=&#8217;newvalue&#8217;,column2=&#8217;nextvalue&#8217; WHERE column3=&#8217;value&#8217;]]></description>
			<content:encoded><![CDATA[<p>While the book only includes a single example on page 117 for the MySQL UPDATE query, it should have also included an example of a multiple column UPDATE. Here is an example:</p>
<blockquote><p>UPDATE table SET column1=&#8217;newvalue&#8217;,column2=&#8217;nextvalue&#8217; WHERE column3=&#8217;value&#8217;</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/tips/mysql-update-multiple-columns-query/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL Find Fields in Table not Found in First Table</title>
		<link>http://www.phpreferencebook.com/tips/mysql-query-not-in-exclusion-table/</link>
		<comments>http://www.phpreferencebook.com/tips/mysql-query-not-in-exclusion-table/#comments</comments>
		<pubDate>Fri, 01 Jan 2010 17:49:28 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=358</guid>
		<description><![CDATA[It took a ton of googling, and it was really hard to find the answer. I had two tables, and wanted to do a MySQL Query that seems like a NOT IN between two tables. There are two tables with one unique field that is the same between the tables. The goal is to find [...]]]></description>
			<content:encoded><![CDATA[<p>It took a ton of googling, and it was really hard to find the answer. I had two tables, and wanted to do a MySQL Query that seems like a NOT IN between two tables. There are two tables with one unique field that is the same between the tables. The goal is to find the rows in the second table that are <strong>not</strong> found in the first table based upon the similar field.</p>
<p>I could attempt to explain this, but someone has already done a very good job of this, so I&#8217;d rather just provide a link to their content. The post is from the author of &#8220;High Performance MySQL&#8221;, Baron Schwartz, and covers writing an <a href="http://www.xaprb.com/blog/2005/09/23/how-to-write-a-sql-exclusion-join/">SQL Exclusion Join</a>. The key area of interest is the section on LEFT OUTER joins. He offers this example query:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> apples<span style="color: #66cc66;">.</span>Variety
<span style="color: #993333; font-weight: bold;">FROM</span> apples
    <span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">OUTER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> oranges
        <span style="color: #993333; font-weight: bold;">ON</span> apples<span style="color: #66cc66;">.</span>Price <span style="color: #66cc66;">=</span> oranges<span style="color: #66cc66;">.</span>Price
<span style="color: #993333; font-weight: bold;">WHERE</span> oranges<span style="color: #66cc66;">.</span>Price <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NULL</span></pre></div></div>

<p>When in doubt, find someone smarter to answer the question for you. <img src='http://www.phpreferencebook.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Thanks Baron!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/tips/mysql-query-not-in-exclusion-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Seven Downloads for Young Web Developers</title>
		<link>http://www.phpreferencebook.com/misc/php-download-beginner-web-developer/</link>
		<comments>http://www.phpreferencebook.com/misc/php-download-beginner-web-developer/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 14:54:27 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[SEO]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=121</guid>
		<description><![CDATA[There are tons of books and tutorials on PHP, MySQL, Zend, and SEO on store shelves. There are even more resources found only on the internet. Sometimes you need something in the middle, content you can download legally and keep handy when you are offline. Below are a few completely free resources that most beginner [...]]]></description>
			<content:encoded><![CDATA[<p>There are tons of books and tutorials on PHP, MySQL, Zend, and SEO on store shelves. There are even more resources found only on the internet. Sometimes you need something in the middle, content you can download legally and keep handy when you are offline. Below are a few completely free resources that most beginner or intermediate web developers (even if you do it just for yourself) will find useful.</p>
<h3>PHP</h3>
<p><strong><em>1)</em> PHP Reference: Beginner to Intermediate PHP5</strong></p>
<p><em>Shameless self-promotion.</em> A reference for many of the functions within PHP that serves as a quick go-to resource for checking syntax and remembering the nuances of many of the functions. It is available as a <a href="http://www.phpreferencebook.com/">PHP book</a> you can purchase in print, however the entire book is released under creative commons and available as a <a href="http://www.phpreferencebook.com/pdf">PHP reference PDF</a>.</p>
<p><strong><em>2)</em> Object Oriented PHP Tutorial in PDF</strong></p>
<p>Provided by killerphp.com and Stefan Mischook, this is a PDF version of his article on the topic of object orientated programming in PHP. It gives a conversational explanation to the basics. More information and the <a href="http://www.killerphp.com/articles/object-oriented-php-tutorial-in-pdf/">OOP PHP PDF</a> is available over on <a href="http://www.killerphp.com">killerphp.com</a>.</p>
<p><strong><em>3)</em> Zend Framework: Surviving the Deep End</strong></p>
<p>The Zend Framework can help developers organize and write more efficient PHP code for large projects and has become one of the top frameworks used online today. <em>From the page&#8230;</em><br />
<blockquote>&#8220;The book was written to guide readers through the metaphorical &#8216;Deep End&#8217;. It&#8217;s the place you find yourself in when you complete a few tutorials and scan through the Reference Guide, where you are buried in knowledge up to your neck but without a clue about how to bind it all together effectively into an application.&#8221;</p></blockquote>
<p> While available online, there is a link to downloading the PDF version in the bottom right. Check out the <a href="http://www.survivethedeepend.com/">Zend Framework survival guide</a>.</p>
<h3>HTML, CSS, AJAX</h3>
<p><strong><em>4)</em> The Woork Handbook</strong></p>
<p>Another compilation of online articles compiled and organized as an offline document. <em>From the page&#8230;</em><br />
<blockquote>&#8220;The Woork Handbook is a free eBook about CSS, HTML, Ajax, web programming, Mootools, Scriptaculous and other topics about web design&#8230; directly from Woork!&#8221;</p></blockquote>
<p> This isn&#8217;t a full study of any single topic, but is filled with tidbits. Grab the <a href="http://woork.blogspot.com/2009/01/woork-handbook.html">Woork Handbook</a>.</p>
<p><strong><em>5)</em> Added Bytes Cheat Sheets (formerly ILoveJackDaniels)</strong></p>
<p>While the site&#8217;s name has changed, the great resources have not. Cheat sheets are designed to cram (in a useful way) tons of information into the front and back of an 8.5&#8243; x 11&#8243; sheet of paper. There is little excuse for not keeping these handy. Grab the <a href="http://www.addedbytes.com/cheat-sheets/">cheat sheets</a> for HTML, CSS, RegEx, Mod_Rewrite, and more.</p>
<h3>MySQL</h3>
<p><strong><em>6)</em> MySQL Manual</strong></p>
<p>All MySQL documentation is available as a downloadable file. Choose from various options for the <a href="http://dev.mysql.com/doc/">MySQL documentation</a>.<br />
<strong><em>Editor&#8217;s Note: </strong>I had trouble finding a good, free, legal resource for MySQL that wasn&#8217;t hyper specific and was user friendly. I hope others have suggestions they can leave in the comments.</em></p>
<h3>SEO (Search Engine Optimization)</h3>
<p><strong><em>7)</em> Beginner&#8217;s Guide to Search Engine Optimization</strong></p>
<p>SEOMoz.org is an amazing website for learning the complex world of SEO with a very clear, user-friendly tone. Besides that, they offer a bunch of tools for helping your new website get better in the eyes of search engines (aka Google). Available as HTML, MS Word, or an OpenOffice document, you can get a copy of the <a href="http://www.seomoz.org/article/beginners-guide-to-search-engine-optimization">Beginner&#8217;s Guide to Search Engine Optimization</a> over on <a href="http://www.seomoz.org/">SEOMoz.org</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/misc/php-download-beginner-web-developer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>mysql_real_escape_string() vs addslashes()</title>
		<link>http://www.phpreferencebook.com/clarifications/mysql_real_escape_string-vs-addslashes/</link>
		<comments>http://www.phpreferencebook.com/clarifications/mysql_real_escape_string-vs-addslashes/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 05:24:08 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Clarifications]]></category>
		<category><![CDATA[addslashes]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[mysql_real_escape_string]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[sql injection]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=10</guid>
		<description><![CDATA[(pgs. 124-125) When describing the function mysql_real_escape_string(), the following note was included: Note: Performs the same functionality as addslashes(). While they are practically identical in their behavior, this is an oversimplification of the extra strength of mysql_real_escape_string(). At this point, I will pass on the following blog post that provides some extra insight into the [...]]]></description>
			<content:encoded><![CDATA[<h4>(pgs. 124-125)</h4>
<p>When describing the function <strong>mysql_real_escape_string()</strong>, the following note was included:</p>
<blockquote><p><em>Note: Performs the same functionality as <strong>addslashes()</strong>.</em></p></blockquote>
<p>While they are practically identical in their behavior, this is an oversimplification of the extra strength of <strong>mysql_real_escape_string()</strong>. At this point, I will pass on the following blog post that provides some extra insight into the comparison of the two variables:<a title="addslashes() Versus mysql_real_escape_string()" href="http://shiflett.org/blog/2006/jan/addslashes-versus-mysql-real-escape-string" target="_blank">addslashes() Versus mysql_real_escape_string()</a> by Chris Shiflett. I was not previously aware of this difference and security loophole.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/clarifications/mysql_real_escape_string-vs-addslashes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

