2009-02-21

MySQL "unable to select database" error

A quick Google search will show a lot of somewhat disjoint solutions to or descriptions of this problem - basically, it's pretty simple - I found it when I set up a "Login" example page using PHP and MySQL. All looked right, but the login failed with a "Select Failed" message, which in turn allowed that the user defined for the web opeation was not authorized by the database, causing the select to fail.

Further investigation led me to insert mysql_error() at the point of the select error, which told me that the problem was that the user had not been added [defined] for the database.

So if a PHP script gets a "Cannot select DB" error on the select statement, make sure the user has been added to the database with the appropriate permissions.