<?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>Antonio Ciccarone &#187; connect</title>
	<atom:link href="http://blog.antoniociccarone.com/tag/connect/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.antoniociccarone.com</link>
	<description>Life, Code and Music</description>
	<lastBuildDate>Tue, 25 Oct 2011 21:39:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Connect to MySQL Function</title>
		<link>http://blog.antoniociccarone.com/connect-to-mysql-function/</link>
		<comments>http://blog.antoniociccarone.com/connect-to-mysql-function/#comments</comments>
		<pubDate>Wed, 16 Jun 2010 21:27:21 +0000</pubDate>
		<dc:creator>Antonio Ciccarone</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[connect]]></category>
		<category><![CDATA[connection]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.antoniociccarone.com/?p=158</guid>
		<description><![CDATA[Connect to a MySQL database.  Download the zip with all related php files. <a href="http://blog.antoniociccarone.com/connect-to-mysql-function/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This is how I connect to a MySQL database.  The following code is based off of three or four separate php files but could <strong>insecurely</strong> be combined into one.  We&#8217;ll be creating functions.php which holds all of the code that the site uses, settings.php which holds the configurable options and a connection.php that connects to the database.  The fourth and obviously optional file is your index.php (or wherever you&#8217;re connecting from)</p>
<p>First, in functions.php we&#8217;ll define our connectSQL.  It takes four arguments and connects or returns an error if unsuccessful</p>
<div id="code">

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p158code1'); return false;">View Code</a> LANGUAGE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1581"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p158code1"><pre class="language" style="font-family:monospace;">function connectSQL($server, $user, $pass, $name) {
	define(&quot;DB_SERVER&quot;, $server);define(&quot;DB_USER&quot;, $user);define(&quot;DB_PASS&quot;, $pass);define(&quot;DB_NAME&quot;, $name);
	$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
	if (!$connection) {
		die(&quot;Database connection failed: &quot; . mysql_error());
	}
	$db_select = mysql_select_db(DB_NAME,$connection);
	if (!$db_select) {
		die(&quot;Database selection failed: &quot; . mysql_error());
	}
}</pre></td></tr></table></div>

</div>
<p>In settings.php we&#8217;ll assign values to the variables.  Always remember to use secure passwords.</p>
<div id="code">

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p158code2'); return false;">View Code</a> LANGUAGE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1582"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p158code2"><pre class="language" style="font-family:monospace;">// Database Configuration
$database['server'] = &quot;localhost&quot;;
$database['user'] = &quot;xxx&quot;;
$database['password'] = &quot;xxx&quot;;
$database['name'] = &quot;xxx&quot;;</pre></td></tr></table></div>

</div>
<p>The actual connection code is now pretty easy to implement.  You can call it whenever and however you&#8217;d like with only one line of code.</p>
<div id="code">

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p158code3'); return false;">View Code</a> LANGUAGE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1583"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p158code3"><pre class="language" style="font-family:monospace;">// Connection to the database via function
connectSQL($database['server'], $database['user'], $database['password'], $database['name']);</pre></td></tr></table></div>

</div>
<p>Finally, to actually use this, simply include the connection.php</p>
<div id="code">

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p158code4'); return false;">View Code</a> LANGUAGE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1584"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p158code4"><pre class="language" style="font-family:monospace;">include 'connection.php';</pre></td></tr></table></div>

</div>
<p><a href="downloads/Connect to MySQL Function.zip"><img src="images/downloadCode.png" border="0" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.antoniociccarone.com/connect-to-mysql-function/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

