<?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>Compiled Thoughts by John Ciacia &#187; mysql</title>
	<atom:link href="http://www.johnciacia.com/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.johnciacia.com</link>
	<description>Science, Technology, and Beyond</description>
	<lastBuildDate>Fri, 06 Jan 2012 15:46:22 +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>Debugging MySQL issues in PHP</title>
		<link>http://www.johnciacia.com/2010/01/12/debugging-mysql-issues-in-php-2/</link>
		<comments>http://www.johnciacia.com/2010/01/12/debugging-mysql-issues-in-php-2/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 18:19:16 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.johnciacia.com/?p=477</guid>
		<description><![CDATA[Possibly the most common issue users have with PHP is working with MySQL. Executing a query requires a three step process: Connecting to your database server. Selecting a database. Executing the query. When a query fails, many users don&#8217;t know where to begin debugging. This guide outlines my debugging procedure. Verifying Database Connectivity 1. A [...]]]></description>
			<content:encoded><![CDATA[<p>Possibly the most common issue users have with PHP is working with MySQL. Executing a query requires a three step process:</p>
<ul>
<li>Connecting to your database server.</li>
<li>Selecting a database.</li>
<li>Executing the query.</li>
</ul>
<p>When a query fails, many users don&#8217;t know where to begin debugging. This guide outlines my debugging procedure.</p>
<h2>Verifying Database Connectivity</h2>
<p>1. A database connection is generally established using <a href="http://us3.php.net/manual/en/function.mysql-connect.php">mysql_connect</a>. As the documentation states: </p>
<blockquote><p>Returns a MySQL link identifier on success, or FALSE on failure.</p></blockquote>
<p>Thus the first thing to check is the value mysql_connect is returning. There are several ways to test the output. Assuming you are using the following line of code to connect to your database<br />
[code lang="php"]$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');[/code]<br />
You can do as the PHP manual suggests:<br />
[code lang="php"]if (!$link) {<br />
    die('Could not connect: ' . mysql_error());<br />
}<br />
echo 'Connected successfully';[/code]<br />
or for something quick and dirty:<br />
[code lang="php"]var_dump($link);[/code]<br />
The former will output <em>Connected successfully</em> on success and the latter will output something similar to <em>resource(2) of type (mysql link)</em> If this is the case, you know mysql_connect is returning a resource and your issue probably has nothing to do with establishing a connection to the database. If the former outputs <em>Could not connect:</em> or the latter outputs <em>bool(false)</em> you have a problem connecting to your database server. In this case you should:</p>
<ul>
<li>Verify your parameter order is correct &#8211; <em>server</em>, <em>username</em>, <em>password</em>.</li>
<li>Verify that the credentials are correct.</li>
<li>Verify your mysql server is actually running&#8230;</li>
</ul>
<p>If you are still having issues connecting to your database, your problem is beyond the discussion of this guide.</p>
<h2>Database Selection</h2>
<p>2. Once you have verified connectivity with the server, verify a database has been selected. Database selection is accomplished using <a href="http://us3.php.net/manual/en/function.mysql-select-db.php">mysql_select_db</a>. This function will return <em>true</em> on success and <em>false</em> on failure. As done in the previous section we want to check which value mysql_select_db is returning. Assuming you have the following code:<br />
[code lang="php"]$db_selected = mysql_select_db('foo', $link);[/code]<br />
You can either use<br />
[code lang="php"]if (!$db_selected) {<br />
    die ('Can\'t use foo : ' . mysql_error());<br />
}[/code]<br />
or simply<br />
[code lang="PHP"]var_dump($db_selected)[/code]<br />
If you receive <em>Can&#8217;t use foo</em> or <em>bool(false)</em> your issue is with selecting your database.</p>
<ul>
<li>Verify the database you are selecting is spelled correctly.</li>
<li>Verify the database exists.</li>
<li>Verify the user you connected to the database server with has access to this particular database.</li>
</ul>
<h2>The Query</h2>
<p>3. If you have successfully established a connection with the database server and have selected a database, and your query still does not work, then the problem is most likely your query itself. Assuming you use are using the following code to query your database:<br />
[code lang="php"]$result = mysql_query('SELECT * WHERE 1=1');[/code]<br />
You can see more information about your error using <a href="http://us3.php.net/manual/en/function.mysql-error.php">mysql_error()</a>. Here is how:<br />
[code lang="php"]$result = mysql_query('SELECT * WHERE 1=1');<br />
if (!$result) {<br />
    die('Invalid query: ' . mysql_error());<br />
}[/code]<br />
This will usually result in a Googleable error or you can refer to the MySQL documentation for the <a href="http://dev.mysql.com/doc/refman/5.1/en/error-handling.html">MySQL error codes</a>. Unfortunately, most queries are not as simple as the one in the example above. They can usually span several lines and almost always contain variables. If this is the case, I echo my query and execute the output it in phpMyAdmin. phpMyAdmin usually gives me an error that is easily understandable, like &#8220;Unknown column name foo.&#8221;<br />
[code lang="php"]$query = "SELECT * FROM foo WHERE id=$id";<br />
die($query);<br />
...[/code]<br />
This will print your query exactly as it is executed. Many times unescaped characters in variables (which may cause <a href="http://forum.codecall.net/security-tutorials/4422-php-sql-injections.html">SQL Injections</a>) will cause your query to fail. Echoing your query will help catch this. Lastly, try not to use a keyword as a database name or column. For example, if you named a column <em>select</em> your query might look like </p>
<div class="quote">SELECT * FROM select WHERE foo=bar</div>
<p> The error here is obvious. MySQL interpreters select as a keyword and not as a column name. To avoid this, use backticks around your table and column names.
<div class="quote">SELECT * FROM `select` WHERE `foo`=&#8217;bar&#8217;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.johnciacia.com/2010/01/12/debugging-mysql-issues-in-php-2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Setting up a LAMP server with Ubuntu</title>
		<link>http://www.johnciacia.com/2009/07/05/setting-up-a-lamp-server-with-ubuntu/</link>
		<comments>http://www.johnciacia.com/2009/07/05/setting-up-a-lamp-server-with-ubuntu/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 03:03:16 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[lamp]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.johnciacia.com/?p=199</guid>
		<description><![CDATA[Ubuntu, known for its ease of use, makes no exception for setting up an apache, mysql, and php stack. Since the release of Feisty Fawn, Ubuntu has come packed with tasksel &#8211; a user interface for installing tasks. 1. At your command prompt, run tasksel as root. john@earth:~$ sudo tasksel 2. Select LAMP server 3. [...]]]></description>
			<content:encoded><![CDATA[<p>Ubuntu, known for its ease of use, makes no exception for setting up an apache, mysql, and php stack. Since the release of Feisty Fawn, Ubuntu has come packed with <strong>tasksel</strong> &#8211; a user interface for installing tasks. </p>
<p>1. At your command prompt, run tasksel as root.</p>
<blockquote><p>john@earth:~$ sudo tasksel</p></blockquote>
<p>2. Select LAMP server<br />
<a href="http://www.johnciacia.com/wp-content/uploads/2009/07/1.png"><img src="http://www.johnciacia.com/wp-content/uploads/2009/07/1-300x198.png" alt="1" title="1" width="300" height="198" class="aligncenter size-medium wp-image-200" /></a></p>
<p>3. Continue the installation by following the prompts.</p>
<p>It works!<br />
<a href="http://www.johnciacia.com/wp-content/uploads/2009/07/4.png"><img src="http://www.johnciacia.com/wp-content/uploads/2009/07/4-300x206.png" alt="4" title="4" width="300" height="206" class="aligncenter size-medium wp-image-201" /></a></p>
<p>One tool I have difficulty living without is phpMyAdmin. From the command prompt type<br />
<blockquote>sudo apt-get install phpmyadmin</p></blockquote>
<p> Continue the installation by following the prompts. If you are using a version of Ubuntu older than 9.04 (Jaunty) you will need to add the following line to /etc/apache2/apache2.conf Continue the installation by following the prompts. Version 9.04 does this automatically. You will be able to access phpMyAdmin by browsing directly to http://<hostname>/phpmyadmin </p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnciacia.com/2009/07/05/setting-up-a-lamp-server-with-ubuntu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Note that the MySQL client library is not bundled anymore!</title>
		<link>http://www.johnciacia.com/2009/05/09/note-that-the-mysql-client-library-is-not-bundled-anymore/</link>
		<comments>http://www.johnciacia.com/2009/05/09/note-that-the-mysql-client-library-is-not-bundled-anymore/#comments</comments>
		<pubDate>Sun, 10 May 2009 03:29:11 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[lamp]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.johnciacia.com/?p=82</guid>
		<description><![CDATA[Unable to get PHP configured to my specifications using the Ubuntu repositories, I decided to install it from source. However, I kept getting the error: Note that the MySQL client library is not bundled anymore! Not wanting to install MySQL from source, I found a package in the Ubuntu repositories that installed the necessary library [...]]]></description>
			<content:encoded><![CDATA[<p>Unable to get PHP configured to my specifications using the Ubuntu repositories, I decided to install it from source. However, I kept getting the error: </p>
<blockquote><p>Note that the MySQL client library is not bundled anymore!</p></blockquote>
<p>Not wanting to install MySQL from source, I found a package in the Ubuntu repositories that installed the necessary library files.</p>
<blockquote><p>sudo apt-get install libmysqlclient15-dev</p></blockquote>
<p>After I installed that package, PHP was able to install successfully. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnciacia.com/2009/05/09/note-that-the-mysql-client-library-is-not-bundled-anymore/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: www.johnciacia.com @ 2012-02-05 09:43:09 -->
