<?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 Starter &#187; MySQL</title>
	<atom:link href="http://phpstarter.net/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://phpstarter.net</link>
	<description>PHP Tips &#38; Tools From Starters to Experts</description>
	<lastBuildDate>Fri, 25 Jun 2010 14:14:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Basic PHP/MySQL Actions</title>
		<link>http://phpstarter.net/2009/02/basic-phpmysql-actions/</link>
		<comments>http://phpstarter.net/2009/02/basic-phpmysql-actions/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 06:00:22 +0000</pubDate>
		<dc:creator>Kurtis</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Beginner]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=357</guid>
		<description><![CDATA[Using databases to store user identification, image information, and so much more for dynamic websites is the easiest way to ease maintenance. MySQL is the most popular choice of database, and in this article, I'll explain the most basic functions: connecting and using a MySQL database and its respective tables.
No related posts.]]></description>
			<content:encoded><![CDATA[<p>Using databases to store user identification, image information, and so much more for dynamic websites is the easiest way to ease maintenance.  MySQL is the most popular choice of database, and in this article, I&#8217;ll explain the most basic functions: connecting and using a MySQL database and its respective tables.<span id="more-357"></span>The first step in using a MySQL database is to make sure your host has the necessities set up.  Typically, your web host&#8217;s control panel will allow the creation of a database/user pair which will allow you to connect.  However, most do not allow the creation of these directly through a PHP script.  Check with your host to see how you can get your MySQL database running.</p>
<p>The next step, and probably the first in your running script is connecting to the user and database, using the following two functions:</p>
<pre class="brush: php">

&lt;?php

...

mysql_connect(host, user, password);

mysql_select_db(database [, link]);

...

?&gt;
</pre>
<p>Again, your host will work with you with respect to establishing the user, password, and database, and <em>host</em> is typically &#8220;localhost&#8221; when accessing the database on the same server as your website, but check with your host if you are having problems.  The link, which is optional, would refer to the variable holding your <strong>mysql_connect()</strong> reference.</p>
<p>Now you have a database ready to go, but without a table, there&#8217;s no place for your streams of data to go, so let&#8217;s set one up.  Most of our queries to MySQL are going to be run through the appropriately named <strong>mysql_query()</strong> function, and the text within will change with each different application.  To create a table, we run a query like this:</p>
<pre class="brush: php">

&lt;?php

//Connect to MySQL before running any queries, every time

mysql_query(&quot;CREATE TABLE table_name(

column_one ATTRIBUTES,

column_two ATTRIBUTES,

...

)&quot;);

mysql_close(link);

?&gt;
</pre>
<p>You should take the time before creating a table to decide what all needs to be stored and what would be the best manner to do so, and then use one of the column types and attributes name <a href="http://dev.mysql.com/doc/refman/5.1/en/create-table.html" target="_blank">at this resource</a>.  Some may seem confusing, but there are plenty of elaborations and tutorials available to describe the uses of each type.  The <strong>mysql_close()</strong> function simply closes our the defined connection and is typically unnecessary as the connection will be closed at the end of the script anyway.</p>
<p>With a table available for queries, we can begin adding and working with our data.  The main abilities available with the <strong>mysql_query()</strong> function our <strong>SELECT</strong>, <strong>SHOW</strong>, <strong>DESCRIBE</strong>, <strong>EXPLAIN</strong>, <strong>INSERT</strong>, <strong>UPDATE</strong>, <strong>DELETE</strong>, and <strong>DROP</strong>.  The first four return a resource (explained in detail in a minute) and the latter four return a boolean value depending on the success.</p>
<p><strong>SHOW</strong>, <strong>DESCRIBE</strong>, and <strong>EXPLAIN</strong> are honestly rarely used within the context of a functioning, user-run website.  They are moreso used on the administrator&#8217;s end and even then are not as functional as the others.  <strong>DELETE</strong> and <strong>DROP</strong> should certainly only be used on the administrator&#8217;s end and probably, again, only through MySQL itself and not through a publicly-available executable file.  Only through proper authentication should someone be able to delete a record or even an entire MySQL table.</p>
<p>Let&#8217;s start with <strong>INSERT</strong>, as that is where you must start to use the other functions.  To insert data into a table, run the function like this:</p>
<pre class="brush: php">

&lt;?php

//Connect to MySQL before running any queries, every time

mysql_query(&quot;INSERT INTO `table_name` (col1, col2, ...)

VALUES(&#039;val1&#039;, &#039;val2&#039;, ...)&quot;) or die(mysql_error());

...

?&gt;
</pre>
<p>The column names in the first set of parentheses should correspond with the correct value in the second set.  You may also opt, with each execution of <strong>mysql_query()</strong> to use the <strong>mysql_error()</strong> function to report an error should one occur, though hopefully you&#8217;ve worked your script well and an error is handled by your own error-checking.  However, some things do slip by and happen unpreventably.</p>
<p>Next is the <strong>SELECT</strong> application.  Just as it sounds, you select records from an identified table and can then use the data however you wish, by displaying it, checking it against variables, etc.  Either way, a <strong>SELECT</strong> application looks like this:</p>
<pre class="brush: php">

&lt;?php

//Connect to MySQL before running any queries, every time

$getRows = mysql_query(&quot;SELECT * FROM `table_name` WHERE `col_name`=&#039;val&#039;&quot;);

while ($result = mysql_fetch_array($getRows)) {

echo $result[&#039;col_name&#039;];

...

}

...

?&gt;
</pre>
<p>The <strong>*</strong> can be replaced with each column you wish to extract, separated by commas, and the WHERE clause is in the query is optional but should be used when looking for particular records.  You should note that <strong>SELECT</strong> returns a resource, so we have to loop through that resource using the <strong>mysql_fetch_array()</strong>, <strong>mysql_fetch_assoc()</strong>, <strong>mysql_fetch_row()</strong> or an appropriate alternative.  Then the data from the records is stored in an array where each column is referenced by its respective name.</p>
<p>To delete a record (or records) use a similar construct to the <strong>SELECT</strong> application, but no looping or variable-calling is necessary.  It simply works through the query:</p>
<pre class="brush: php">

&lt;?php

//Connect to MySQL before running any queries, every time

mysql_query(&quot;DELETE FROM `table_name` WHERE `col_name`=&#039;val&#039;&quot;);

...

?&gt;
</pre>
<p>To delete an entire table, use the <strong>DROP</strong> application, though realize that every record will be obliterated:</p>
<pre class="brush: php">

&lt;?php

//Connect to MySQL before running any queries, every time

mysql_query(&quot;DROP TABLE `table_name`&quot;);

...

?&gt;
</pre>
<p>Lastly, editing records is fairly easy though it can be more cumbersome.  Using the <strong>UPDATE</strong> application like so can allow for the altering of preexisting rows.</p>
<pre class="brush: php">

&lt;?php

//Connect to MySQL before running any queries, every time

mysql_query(&quot;UPDATE `table_name`

SET `col_name` = &#039;val&#039;, `col2_name` = &#039;val2&#039;, ...

WHERE `col_name` = &#039;val&#039;&quot;);

...
?&gt;
</pre>
<p>You can change as many columns as you need, separating each with a comma, but be careful with spacing because the success of the update can depend highly on the spacing as I have seen in the past, though I can&#8217;t find a good reference on what the exact spacing should look like.</p>
<p>With these basic applications in tow, little cannot be accomplished with your MySQL database.  With a dynamic, user-driven website these will certainly be integrated into your website.  To keep a more watchful eye on your database and perform certain one-time actions on your tables, you may also choose to employ a GUI like <a href="http://www.phpmyadmin.net" target="_blank">PHPMyAdmin</a> or something similar which your host may already have available through the control panel.</p>
<p>Happy querying!</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2009/02/basic-phpmysql-actions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing Secure SQL Queries</title>
		<link>http://phpstarter.net/2008/02/writing-secure-sql-queries/</link>
		<comments>http://phpstarter.net/2008/02/writing-secure-sql-queries/#comments</comments>
		<pubDate>Mon, 25 Feb 2008 17:50:27 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Intermediate]]></category>

		<guid isPermaLink="false">http://wp.pr0gr4mm3r.com/mysql/writing-secure-sql-queries/</guid>
		<description><![CDATA[Writing secure SQL queries can be one of the most important factors in your site&#8217;s security, yet I see so many people that don&#8217;t do it. So many programmers write queries that &#8220;just work&#8221; taking little consideration of the malicious potential of unsecured code &#8211; SQL queries included. This post is going to show some [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>Writing secure SQL queries can be one of the most important factors in your site&#8217;s security, yet I see so many people that don&#8217;t do it. So many programmers write queries that &#8220;just work&#8221; taking little consideration of the malicious potential of unsecured code &#8211; SQL queries included.  This post is going to show some examples of bad queries in MySQL and how to correct them.</p>
<p><span id="more-6"></span></p>
<p>I will start out with the most common mistake that I see:</p>
<blockquote><p>SELECT * FROM Users WHERE user = $_POST['user'] &amp;&amp; password = $_POST['password']</p></blockquote>
<p>The query above in itself is correct.  Assuming the HTML form posts the correct data, the query will correctly process the information and find whether the website user provided the correct credentials to sign in to their account.</p>
<p>&#8230;but what if I, being the malicious user that I am (not really),  inserted the following data?</p>
<blockquote><p>User: admin<br />
Pass: something&#8217; or &#8216;x&#8217;='x</p></blockquote>
<p>That would make the query equal to this:</p>
<blockquote><p>SELECT * FROM Users WHERE user = &#8216;<strong>admin</strong>&#8216; &amp;&amp; password = &#8216;<strong>something&#8217; or &#8216;x&#8217;='x</strong>&#8216;</p></blockquote>
<p>Since &#8216;x&#8217; always equals &#8216;x&#8217;, the following query would grant me access as the user, admin!  It sound like a simple attack, and surely web database programmers know to protect themselves from attacks like this.  The sad truth is that this <a href="http://en.wikipedia.org/wiki/SQL_injection#Real-world_examples">happens all the time</a>.</p>
<p>If you are a programmer that writes SQL queries, I hope you know about this method of exploitation.  If you don&#8217;t, please, please, please read over the following articles, because it&#8217;s critical to write queries that go beyond &#8220;just working&#8221;.  You <strong>must</strong> make them secure.</p>
<p><strong>Further Reading</strong></p>
<p>Typing &#8220;sql injection&#8221; into Google returns several good articles, and here are a few of them that are worth reading over.</p>
<p><a href="http://unixwiz.net/techtips/sql-injection.html">SQL Injection Attacks by Example</a> &#8211;  The people from unixwiz.net were asked to review an intranet site for a customer.  The site had some exploitable areas, and they show what steps they took to compromise an admin-level account.  It&#8217;s very interesting to know what malicious users go through to gain unauthorized access to your site.  By knowing this, you can help keep them out.</p>
<p><a href="http://en.wikipedia.org/wiki/SQL_injection">SQL Injection</a> &#8211; A definition and couple examples from Wikipedia.</p>
<p><a href="http://xkcd.com/327/">Exploits of a Mom</a> &#8211; Ok, so it&#8217;s not an informative article, but it&#8217;s funny.</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2008/02/writing-secure-sql-queries/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

