First time using mysql, I keep getting this error:

Access denied for user 'user'@'localhost' to database 'databaseName'

Its probably something easy but help would be appretiated.

$conn = mysql_connect('localhost', 'user', 'xxxxxx');
if (!$conn) 
    {die('Could not connect: ' . mysql_error() . '<br>');
    }
else 
    {echo 'Connection to mysql server made <br>';
    }
$db = mysql_select_db('databaseName', $conn);
if (!$db)
    {die('Cannot connect to database: ' . mysql_error() . '<br>');
    }
else
    {echo 'Connected to database'; 
    }

    Sorry I should have included my html output.

    Connection to mysql server made
    Cannot connect to database: Access denied for user 'user'@'localhost' to database 'databaseName'

      Heres how I always connect, yes ok you cant see which is wrong, user/pass or db connecting. But if the details are right and db is working it connects you. Thats the main thing right??

      anyway try this:

      $db = mysql_connect('host','user','pass');
      mysql_select_db('db_name', $db) or die( "Unable to select database, 
      		  make sure it exists and that the username and password are 
      		  correct and then run this page again."); 
      

      I use this in a function but you could also assign all to a variable, in which case it would look like this:

      $db = mysql_connect('host','user','pass');
      $connect = mysql_select_db('db_name', $db) or die( "Unable to select database, 
      		  make sure it exists and that the username and password are 
      		  correct and then run this page again."); 
      

      then on the page you wish to connect you just type $connect;
      or if you use a function : function_name();

      Hope this helps
      RT

        You are using the wrong username and/or password combination if this is a local server and a new install then the default for MySQL is

        <?php//always use this not <?
        $user='root';
        $$host='localhost';
        $connect = mysql_connect($host,$user)
          or die("Failed to connect to the server MySQL said: ".mysql_error());
        $dbselect = mysql_select_db('mydatabase')
          or die("Database selection failed MySQL said: ".mysql_error())";
        ...
        ?>
          Write a Reply...