<?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>Webmaster-Source &#187; database</title>
	<atom:link href="https://www.webmaster-source.com/tag/database/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.webmaster-source.com</link>
	<description>Useful Resources For Webmasters</description>
	<lastBuildDate>Thu, 24 Aug 2017 02:01:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Getting Your Feet Wet with PDO and Migrating Old MySQL Code</title>
		<link>https://www.webmaster-source.com/2011/08/05/getting-your-feet-wet-with-pdo-and-migrating-old-mysql-code/</link>
		<comments>https://www.webmaster-source.com/2011/08/05/getting-your-feet-wet-with-pdo-and-migrating-old-mysql-code/#comments</comments>
		<pubDate>Fri, 05 Aug 2011 12:33:16 +0000</pubDate>
		<dc:creator><![CDATA[Matt]]></dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL injection]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=4213</guid>
		<description><![CDATA[You may have heard that the old MySQL extension for PHP is going to eventually be deprecated in favor of the newer (and potentially more secure) MySQLi and PDO extensions. You&#8217;re going to need to update your old code sooner or later, so why not make it sooner? I&#8217;m going to recommend PDO over MySQLi [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>You may have heard that the old MySQL extension for PHP is going to <a href="http://www.webmaster-source.com/2011/07/25/php-to-deprecate-mysql-extension-in-favor-of-mysqli-and-pdo/">eventually be deprecated</a> in favor of the newer (and potentially more secure) MySQLi and PDO extensions. You&#8217;re going to need to update your old code sooner or later, so why not make it sooner?</p>
<p>I&#8217;m going to recommend <a href="http://php.net/manual/en/book.pdo.php">PDO</a> over <a href="http://www.php.net/manual/en/book.mysqli.php">MySQLi</a> simply because it&#8217;s available on more systems, and it&#8217;s syntax may be a little bit easier to learn for newbies. PDO has been bundled with the main PHP distribution since PHP 5.1, and has been in PECL even longer, while MySQLi has only been included since 5.3. Whichever you use is up to personal preference and project requirements of course, but I will be sticking with PDO for the duration of this tutorial.<span id="more-4213"></span></p>
<p>Suppose you have a simple bit of PHP that executes a MySQL query and ouputs a list of items to screen. It might look something like this:</p>
<pre class="brush: php; title: ; notranslate">
    $db = mysql_connect(&quot;localhost&quot;, &quot;username&quot;, &quot;password&quot;);
    mysql_select_db(&quot;database&quot;, $db);

    $string = mysql_real_escape_string($string, $db);
    $query = &quot;SELECT * FROM my_table WHERE item_cat='&quot;.$string.&quot;' ORDER BY item_date DESC LIMIT 5&quot;;
    $result = mysql_query($query);

    if ( mysql_num_rows($result) &gt; 0 ) {
        while ( $row = mysql_fetch_assoc($result) ) {
            echo $row['item_title'] . '&lt;br /&gt;';
        }
    }
</pre>
<p><a href="http://xkcd.com/327/"><img style=' float: right; padding: 4px; margin: 0 0 2px 7px;'  class="alignright size-full wp-image-4214" title="XKCD: Bobby Tables" src="//www.webmaster-source.com/wp-content/uploads/xkcd-bobby-tables.png" alt="" width="124" height="147" /></a>You&#8217;re escaping any input from a third party with mysql_real_escape_string() to prevent injection attacks, I hope. <a href="http://en.wikipedia.org/wiki/Sql_injection">SQL injection</a> is one of the most common ways data is stolen or destroyed by attackers, and it&#8217;s also fairly easy to discourage. Escaping input, while not necessarily 100% effective, should prevent most injection attacks. (And it&#8217;s pretty much your only choice with the old MySQL extension.) A better solution is parameterized statements, which we&#8217;ll get to later.</p>
<p>If your server has PDO installed (which is probably the case if you have PHP 5.1 or greater), migrating is fairly easy. The basic principles are the same, though the syntax differs just a bit. It&#8217;s actually a bit cleaner and more object-oriented.</p>
<p>The example above would look something like this:</p>
<pre class="brush: php; title: ; notranslate">
$db = new pdo(&quot;mysql:host=localhost;dbname=database_name&quot;, &quot;username&quot;, &quot;password&quot;);
$string = $db-&gt;quote($string);
$query = &quot;SELECT * FROM my_table WHERE item_cat=$string ORDER BY item_date DESC LIMIT 5&quot;;
$result = $db-&gt;query($query);

if ($result != false) {
    while ( $row = $result-&gt;fetch(PDO::FETCH_ASSOC) ) {
        echo $row['item_title'] . '&lt;br /&gt;';
    }
}

$result  = null;
</pre>
<p>Doesn&#8217;t that look nicer? Feel free to take a moment to appreciate the object-oriented goodness.</p>
<p>The biggest difference is probably the connection line. The &#8220;DSN&#8221; syntax used to connect to the database might look a bit strange at first, but it&#8217;s an important part of PDO. Since PDO can connect to other types of databases besides MySQL (e.g. PostgreSQL and SQLite), it uses a fairly standard connection string that specifies the server type besides the database name.</p>
<p>Escaping strings works essentially the same, but the syntax is slightly different. You need to remember to not put quotes around the variable in your SQL string, as the PDO::quote() method will do it for you. If you compare the SQL statements in the two examples, you&#8217;ll see the lack of quotations in the PDO example.</p>
<p>One gotcha to be aware of with PDO is that you need to set your $result variable to <em>null</em> if you intend to reuse it later on in the same script. You can end up with some weird results if you don&#8217;t. So just get in the habit of setting it to <em>null</em> or using unset() on it.</p>
<p>Now how about those <a href="http://www.php.net/manual/en/pdo.prepare.php">parameterized statement</a> things? They&#8217;re a way of ensuring that your code will be immune to SQL injection. Instead of mashing PHP strings together and passing the resulting query to the database engine, you keep the query and the potentially dangerous data separate. Placeholders are put in the query, and the data assigned to those placeholders is sent along with it.</p>
<pre class="brush: php; title: ; notranslate">
$db = new pdo(&quot;mysql:host=localhost;dbname=database_name&quot;, &quot;username&quot;, &quot;password&quot;);

$sql = &quot;SELECT * FROM my_table WHERE item_cat= :mystring ORDER BY item_date DESC LIMIT 5&quot;;
$statement = $db-&gt;prepare($sql);

$statement-&gt;execute(array(
    ':mystring' =&gt; $my_string
));

$result = $statement-&gt;fetchAll();

if ($result != false) {
    while ( $row = $result-&gt;fetch(PDO::FETCH_ASSOC) ) {
        echo $row['item_title'] . '&lt;br /&gt;';
    }
}

$result  = null;
</pre>
<p>Parameterized queries may have some performance issues on MySQL versions prior to 5.1, but they shouldn&#8217;t have any significant disadvantages on more modern systems. Security-wise, they&#8217;re considered to be better than simply escaping strings.</p>
<p>I hope this little guide has been sufficient to get you started with PDO. You will probably want to c<a href="http://php.net/manual/en/book.pdo.php">heck out the documentation</a> to see what else it can do. While you don&#8217;t necessarily need to go out and update your old code right away—the PHP devs aren&#8217;t going to drop support for the old ext/mysql extension for quite a long time, if they ever get rid of it completely—but you should definitely familiarize yourself with the newer techniques and use them in new projects.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.webmaster-source.com/2011/08/05/getting-your-feet-wet-with-pdo-and-migrating-old-mysql-code/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WordPress&#8217;s WPDB Class</title>
		<link>https://www.webmaster-source.com/2009/10/19/wordpresss-wpdb-class/</link>
		<comments>https://www.webmaster-source.com/2009/10/19/wordpresss-wpdb-class/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 11:42:43 +0000</pubDate>
		<dc:creator><![CDATA[Matt]]></dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=2647</guid>
		<description><![CDATA[WordPress has a class, $wpdb, that contains several useful functions for manipulating the database. (The $wpdb object is a global variable that WordPress automatically creates.) This is used throughout the core, and by plugin and theme developers to easily run custom MySQL operations. It even has functions for building and executing prepared statements. You can [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>WordPress has a class, $wpdb, that contains several useful functions for manipulating the database. (The $wpdb object is a global variable that WordPress automatically creates.) This is used throughout the core, and by plugin and theme developers to easily run custom MySQL operations. It even has functions for building and executing prepared statements.</p>
<p>You can find a nice introduction and examples <a href="http://wpengineer.com/wordpress-database-functions/">at WP Engineer</a>. Here&#8217;s a sample of how you would go about changing the title of a post with a certain ID:</p>
<pre class="brush: php; title: ; notranslate">
$wpdb-&gt;update( $wpdb-&gt;posts, array( 'post_title' =&gt; $new_post_title ),
 array( 'ID' =&gt; $the_post_id ) );
</pre>
<p>That&#8217;s just scratching the surface, but it gives a good idea of how the database functions work.</p>
<p>For even more in-depth reading, there&#8217;s a good <a href="http://codex.wordpress.org/Function_Reference/wpdb_Class">Codex page</a> on the subject as well.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.webmaster-source.com/2009/10/19/wordpresss-wpdb-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning MySQL [Book Review]</title>
		<link>https://www.webmaster-source.com/2008/10/08/learning-mysql-book-review/</link>
		<comments>https://www.webmaster-source.com/2008/10/08/learning-mysql-book-review/#comments</comments>
		<pubDate>Wed, 08 Oct 2008 10:06:34 +0000</pubDate>
		<dc:creator><![CDATA[Matt]]></dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[books]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=899</guid>
		<description><![CDATA[Almost anyone who&#8217;s played around with PHP before has run into the subject of databases. MySQL databases are the most common method of storing massive amounts of data to later be sorted through and retrieved for display via script. WordPress stores all of it&#8217;s posts and settings in MySQL tables, forum scripts are powered by [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.amazon.com/gp/product/0596008643?ie=UTF8&amp;tag=webmasterso0d-20&amp;link_code=as3&amp;camp=211189&amp;creative=373489&amp;creativeASIN=0596008643"><img style=' float: left; padding: 4px; margin: 0 7px 2px 0;'  class="alignleft" title="Learning MySQL" src="http://images.amazon.com/images/P/0596008643.jpg" alt="" width="122" height="160" /></a>Almost anyone who&#8217;s played around with PHP before has run into the subject of databases. MySQL databases are the most common method of storing massive amounts of data to later be sorted through and retrieved for display via script. WordPress stores all of it&#8217;s posts and settings in MySQL tables, forum scripts are powered by databases, sites like Amazon, YouTube, and of course IMDB make extensive use of databases. If you think about it, most modern websites are just pretty user interfaces for databases.</p>
<p>As common as they may be, databases sure are mysterious critters. It look me awhile to grasp the concept of them at first, and even longer for me to pick-up the skills required to make use of them. In addition to knowing a scripting language like PHP or Perl, and how to submit a query to the DB server, you also need to know the SQL language.</p>
<p>I just finished a great book on MySQL. <a href="http://www.amazon.com/gp/product/0596008643?ie=UTF8&amp;tag=webmasterso0d-20&amp;link_code=as3&amp;camp=211189&amp;creative=373489&amp;creativeASIN=0596008643"><em>Learning MySQL</em></a> by Seyed M.M. Tahaghoghi and Hugh Williams is a comprehensive and well-explained book that teaches you from the ground up about MySQL databases and how to work with them. It introduces the concept of a database, walks you through installing the MySQL server software (if you&#8217;re not already running it), explains querying, then moves on to the real meat of the book: Structured Query Language, or SQL. It covers basic SELECTS and INSERTs, JOINS, nested queries, table and column types, and really everything you need to know to get started.</p>
<p>Following chapters cover topics like using PHP or Perl to interact with databases (as opposed to using a MySQL prompt) and securing web applications. There are also sections on planning database structures optimally, optimizing performance.</p>
<p>The book is written in a manner that should make it easy to follow, it&#8217;s full of code examples to try out, and overall is enough to give you a general to intermediate knowledge of MySQL. It also works as a handy reference.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.webmaster-source.com/2008/10/08/learning-mysql-book-review/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/


Served from: www.webmaster-source.com @ 2026-06-09 19:27:31 by W3 Total Cache
-->