Notice: Could not select the database!
MySQL Error: Access denied for user 'username'@'localhost' to database 'database' in /home/site/mysql_connect.php on line 11

Above is the error message I get, does anyone know what that may mean? I created a mysql_connect INC file and I put it in a top level directory, now I want to use it to access a MySql database using PHP and I get the error message.

Below is part of the mysql_connect.php file

<?php //mysql_connect.php

DEFINE ('DB_USER', 'username');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'database');

if ($dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD)) {
if (!mysql_select_db (DB_NAME)) {
//echo 'ERROR Selecting Database';
trigger_error("Could not select the database!\n
MySQL Error: " . mysql_error());
exit();
}
} else {
//echo 'ERROR connecting to database';
trigger_error("Could not connect to MySQL!\n
MySQL Error: " . mysql_error());
exit();
}

    Hey

    I take it you're running your database and web server on your local machine?

    If thats the case, try creating a new user with a blank password and see if that user allows you to connect.

    If it does then search the MySql web site, there is a fix to do with encrypted passwords that may help. If its just for development then you may consider just usng the user with no password if this doesn't present a security risk

    Regards

    Tim

      I am running the site at a hosting company. From the documentation they have, I can run localhost even on their servers. When I ran something similar on my local machine it worked, but I had the mysql_connect.php file in the same home directory, I don't want that anymore since I need security, I now need to keep the file in a top level directory where noone can reach it.

      I'm new to PHP, so that's why it's really difficult to figure these kinds of errors out.. hmmmm........

        Hey

        It sounds like its the mysql_select_db() which is failing. does the database exists?

        $db = mysql_connect("localhost", "username", "password") or die("Unable to connect") ;
        
        $db_list = mysql_list_dbs($db);
        
        while ($row = mysql_fetch_object($db_list)) {
             echo $row->Database . "<br>";
        }
        

        This will show all the database available and will test if you are able to connect to the mysql_server.

        I dont think the issue is to do with the location of the file, as long as ur including it correctly. Besides it wouldn't be giving you an error if it couldn't access the file.

        If that works and the database exists you need to check that the user has rights to that database (probably through ur host's software) and remember that PHP is case sensitive so check spellings etc.

        Hope this helps
        Tim

          AHHHH!!!!!!

          Silly me, all my code was looking good, but like you said, I used your code and some other debugging code from PHP in a Nutshell book and process of elimination I figured out that I had a user, database, tables all setup, but the user was not applied to the actual database. I missed that step. OMG, simple things like that driving me nutts.

          Thank you for your help.

            Write a Reply...