<?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>WLScripting.com &#187; MySQL</title>
	<atom:link href="http://www.wlscripting.com/tutorial/category/mysql/feed" rel="self" type="application/rss+xml" />
	<link>http://www.wlscripting.com</link>
	<description>PHP coding tutorials</description>
	<lastBuildDate>Wed, 10 Mar 2010 23:02:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9-beta-1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Common queries</title>
		<link>http://www.wlscripting.com/tutorial/44</link>
		<comments>http://www.wlscripting.com/tutorial/44#comments</comments>
		<pubDate>Sat, 17 Mar 2007 18:10:08 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.wlscripting.com/tutorial/44</guid>
		<description><![CDATA[Useful information about running queries in MySQL with examples.]]></description>
			<content:encoded><![CDATA[<p>When coding in PHP you may at some point need to run queries in a MySQL database. Just like many other database applications, MySQL has several types of queries to perform adding, updating and removing of data.</p>
<p>All of the various types of queries are processed by the mysql_query() function.</p>
<p>To run an insert query:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-var">$sql</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">INSERT INTO `users` (`UserID`, `name`, `email`) VALUES (NULL, '</span><span class="hl-var">$name</span><span class="hl-string">', '</span><span class="hl-var">$email</span><span class="hl-string">');</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
</span><span class="hl-identifier">mysql_query</span><span class="hl-brackets">(</span><span class="hl-var">$sql</span><span class="hl-brackets">) </span><span class="hl-reserved">or die</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">Unable to process query: </span><span class="hl-quotes">'</span><span class="hl-code">.</span><span class="hl-identifier">mysql_error</span><span class="hl-brackets">())</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>Here we are placing the query into the $sql variable, the second line then processes the query. If the query could not be run, the script exits and shows the error that was produced. We are inserting this data into the users table which has columns of UserID, name, and email.</p>
<p>To update a record in the users table you would run:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-var">$sql</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">UPDATE `users` SET `name` = '</span><span class="hl-var">$name</span><span class="hl-string">', `email` = '</span><span class="hl-var">$email</span><span class="hl-string">' WHERE `UserID` = </span><span class="hl-var">$_POST</span><span class="hl-string">[id] LIMIT 1</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
</span><span class="hl-identifier">mysql_query</span><span class="hl-brackets">(</span><span class="hl-var">$sql</span><span class="hl-brackets">) </span><span class="hl-reserved">or die</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Unable to process query: </span><span class="hl-quotes">&quot;</span><span class="hl-code"> . </span><span class="hl-identifier">mysql_error</span><span class="hl-brackets">())</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>Here you specify the table right after the UPDATE and then specify all fields you wish to update after the SET. The last half of the query we are specifying which record we wish to update and limiting the update to only one record. </p>
<p>Now to delete a record or multiple records, you would use:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-var">$sql</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">DELETE FROM users WHERE UserID = '</span><span class="hl-var">$UserID</span><span class="hl-string">' LIMIT 1</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
</span><span class="hl-identifier">mysql_query</span><span class="hl-brackets">(</span><span class="hl-var">$sql</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>This is a very short query that shows the table which we would like to delete a record from and the criteria that we would like to remove. Since we only wish to remove one record, LIMIT 1 is added to make sure we only remove one record.</p>
<p>Anytime that you run a query, you need to make sure that you escape all of your user input variables. If you fail to do this, you risk the chance of somebody performing an SQL injection on your database. One of the easiest ways to perform this is to use the mysql_escape_string() function which preps the variables for use in your query.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wlscripting.com/tutorial/44/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Connect to a MySQL database</title>
		<link>http://www.wlscripting.com/tutorial/43</link>
		<comments>http://www.wlscripting.com/tutorial/43#comments</comments>
		<pubDate>Thu, 15 Mar 2007 22:45:13 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.wlscripting.com/tutorial/43</guid>
		<description><![CDATA[Very basic MySQL database connection code.]]></description>
			<content:encoded><![CDATA[<p>Leaning to work with databases in your PHP starts with understanding on how to connect to the server with PHP. Using only a few lines of code you can easily connect to a database.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-reserved">define</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">SQL_SERVER</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">localhost</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// Specify the server address for the MySQL server, localhost is standard
</span><span class="hl-reserved">define</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">SQL_USERNAME</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">root</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// Specify server username, replace root with your username
</span><span class="hl-reserved">define</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">SQL_PASSWORD</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">password</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// Specify the server password, replace password with your password
</span><span class="hl-reserved">define</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">SQL_DATABASE</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">testing</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// Specify the database name, replace testing with your database name

// Attempt to connect to the server using the constants(variables) specified above
</span><span class="hl-var">$db</span><span class="hl-code"> = @</span><span class="hl-identifier">mysql_connect</span><span class="hl-brackets">(</span><span class="hl-identifier">SQL_SERVER</span><span class="hl-code">, </span><span class="hl-identifier">SQL_USERNAME</span><span class="hl-code">, </span><span class="hl-identifier">SQL_PASSWORD</span><span class="hl-brackets">) </span><span class="hl-reserved">or die</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">Could not connect to the database server</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-comment">// Now try connecting to the database in SQL_DATABASE
</span><span class="hl-var">$link</span><span class="hl-code"> = @</span><span class="hl-identifier">mysql_select_db</span><span class="hl-brackets">(</span><span class="hl-identifier">SQL_DATABASE</span><span class="hl-code">, </span><span class="hl-var">$db</span><span class="hl-brackets">) </span><span class="hl-reserved">or die</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">Could not connect to the database name specified</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>In the code above, I have used <a href="http://php.net/define">define()</a> function to create variables of the MySQL server, username, password and database name. I then use these variables in the next lines of code that actually connects to the server.</p>
<p>There are two types of error handling here in this piece of code. The first is the @, placing this before a function hides any kind of error messages that the two functions could display. The second part is the or die() addition at the end of each connect function. Putting this here stops the rest of the code from being executed and shown to the client.</p>
<p>If when you execute this code it does not show any errors, you are then connected to your database server and can use the mysql_query() function or any other mysql function to manipulate your database.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wlscripting.com/tutorial/43/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
