<?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>Lance Williams &#187; MySQL</title>
	<atom:link href="http://lancewilliams.us/category/techy/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://lancewilliams.us</link>
	<description>my blog</description>
	<lastBuildDate>Fri, 17 Jul 2009 21:21:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using a variable for your MySQL database</title>
		<link>http://lancewilliams.us/techy/using-a-variable-for-your-mysql-database/</link>
		<comments>http://lancewilliams.us/techy/using-a-variable-for-your-mysql-database/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 00:18:39 +0000</pubDate>
		<dc:creator>imaginos</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Techy]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://i.maginos.com/?p=13</guid>
		<description><![CDATA[So, I&#8217;m working on a site making a bunch of changes.  I need to change the name of a database and well, to make a long story short, it&#8217;s called by name throughout all the files.  So I thought I&#8217;d do a quick post on how to use a variable name in place of the [...]]]></description>
			<content:encoded><![CDATA[<p>So, I&#8217;m working on a site making a bunch of changes.  I need to change the name of a database and well, to make a long story short, it&#8217;s called by name throughout all the files.  So I thought I&#8217;d do a quick post on how to use a variable name in place of the MySQL database name.  I had to figure it out myself, and had a hard time finding anywhere how to do it, although it&#8217;s not that difficult.</p>
<p>First off, you should have a config file that&#8217;s being called from every page in your site.  This way you can define some variables that you&#8217;ll be able to access throughout your site.  This is pretty common and is in fact the case on the site I&#8217;m working on.</p>
<p>Your config file should look something like this:</p>
<p><code>&lt;?php</p>
<p>$dbhost = 'localhost';<br />
$dbuser = 'mrcool';<br />
$dbpass = 'coolpassword';<br />
$dbname = 'database_name';</p>
<p>$dbh = mysql_connect($dbhost,$dbuser,$dbpass)<br />
or die (mysql_error());</p>
<p>$setts['sitename'] = "My Super Cool Site";<br />
$setts['siteurl'] = "http://www.mysupercoolsite.com/";<br />
$setts['email'] = "me@mysupercoolsite.com";<br />
$setts['default_theme'] = "CoolTheme";<br />
$setts['db_name'] = $dbname;</p>
<p>?&gt;</code></p>
<p>This covers the basics and allows you to do a lot of cool things with your site.  Anywhere you need to put the base url, you can just use $setts['siteurl'] instead and it makes your whole site portable.  And, as the point of this post, you can use $setts['db_name'] in place of the actual name anywhere you need to.  You can even go one more and name all your tables as variables here, so they can be changed in one spot and affect the whole site.</p>
<p>Ok, last part here.  How to use the variable in place of the name:</p>
<p>Take:</p>
<p>$query = mysql_query(&#8220;INSERT INTO database_name.tblName (first, second, third) VALUES<br />
(&#8216;First Value&#8217;,'Second Value&#8217;,'Third Value&#8217;)&#8221;) or die(mysql_error());</p>
<p>And instead use:</p>
<p>$query = mysql_query(&#8220;INSERT INTO &#8221; . $setts['db_name'] . &#8220;.tblName (first, second, third) VALUES<br />
(&#8216;First Value&#8217;,'Second Value&#8217;,'Third Value&#8217;)&#8221;) or die(mysql_error());</p>
<p>It&#8217;s just simple concatenation.  You have to close the quotes with &#8221; then use the . as the connector, put in your variable name and then another . to add the rest which needs to have the quotes started again.</p>
<p>Hope that helps someone <img src='http://lancewilliams.us/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://lancewilliams.us/techy/using-a-variable-for-your-mysql-database/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
