Posts Tagged ‘mysql’

Debugging MySQL issues in PHP

12. January 2010

3 Comments »

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’t know where to begin debugging. This guide outlines my debugging procedure.

Verifying Database Connectivity

1. A database connection is generally established using mysql_connect. As the documentation states:

Returns a MySQL link identifier on success, or FALSE on failure.

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

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');

You can do as the PHP manual suggests:

if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';

or for something quick and dirty:

var_dump($link);

The former will output Connected successfully on success and the latter will output something similar to resource(2) of type (mysql link) 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 Could not connect: or the latter outputs bool(false) you have a problem connecting to your database server. In this case you should:

  • Verify your parameter order is correct – server, username, password.
  • Verify that the credentials are correct.
  • Verify your mysql server is actually running…

If you are still having issues connecting to your database, your problem is beyond the discussion of this guide.

Database Selection

2. Once you have verified connectivity with the server, verify a database has been selected. Database selection is accomplished using mysql_select_db. This function will return true on success and false 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:

$db_selected = mysql_select_db('foo', $link);

You can either use

if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}

or simply

var_dump($db_selected)

If you receive Can’t use foo or bool(false) your issue is with selecting your database.

  • Verify the database you are selecting is spelled correctly.
  • Verify the database exists.
  • Verify the user you connected to the database server with has access to this particular database.

The Query

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:

$result = mysql_query('SELECT * WHERE 1=1');

You can see more information about your error using mysql_error(). Here is how:

$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

This will usually result in a Googleable error or you can refer to the MySQL documentation for the MySQL error codes. 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 “Unknown column name foo.”

$query = "SELECT * FROM foo WHERE id=$id";
die($query);
...

This will print your query exactly as it is executed. Many times unescaped characters in variables (which may cause SQL Injections) 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 select your query might look like

SELECT * FROM select WHERE foo=bar

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.

SELECT * FROM `select` WHERE `foo`=’bar’

Setting up a LAMP server with Ubuntu

5. July 2009

No Comments »

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 – a user interface for installing tasks.

1. At your command prompt, run tasksel as root.

john@earth:~$ sudo tasksel

2. Select LAMP server
1

3. Continue the installation by following the prompts.

It works!
4

One tool I have difficulty living without is phpMyAdmin. From the command prompt type

sudo apt-get install phpmyadmin

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:///phpmyadmin

Note that the MySQL client library is not bundled anymore!

9. May 2009

No Comments »

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 files.

sudo apt-get install libmysqlclient15-dev

After I installed that package, PHP was able to install successfully.