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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amalahmed
    New Member
    • Jan 2011
    • 1

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

    hi guys

    i have one error in php that coneect to the mysql database

    the conecction is ok

    but the selected the database is not work

    the erorr is( Access denied for user ''@'localhost' to database )

    Code:
    <?php
    
    $link = mysql_connect('localhost', '', '');
    if (!$link) {
        die('Not connected : ' . mysql_error());
    }
    
    // make aaa the current db
    $db_selected = mysql_select_db('amal', $link);
    if (!$db_selected) {
        die ('Can\'t use amal : ' . mysql_error());
    }
    ?>
    so plz anyone help me
    Last edited by Dormilich; Jan 15 '11, 08:08 PM. Reason: please use [code] [/code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I doubt that the connection is OK, since you get an Access Denied error, which is a typical connection error. you should pass an existing login name, an empty string is unlikely to work.

    Comment

    • Sudaraka
      New Member
      • Jan 2011
      • 55

      #3
      If you are trying to use the MySQL connection settings specified in the php.ini, you should omit the parameters to the mysql_connect altogether and let it fetch the defaults from php.ini

      Otherwise like Dormilich said, you must specify the MySQL Username, and the matching Password.

      Comment

      • Rekha Kumaran
        New Member
        • Jan 2011
        • 30

        #4
        Try to use this

        mysql_connect(" ip_address","us ername","passwo rd") or die("could not connected".mysq l_error());

        mysql_select_db ("databasename" ) or die(mysql_error ());

        Comment

        • mfaisalwarraich
          New Member
          • Oct 2007
          • 194

          #5
          try this:
          Code:
          $con = mysql_connect('hostname/ip-address', 'db_username','db_password') or die('could not connect to database'. mysql_error());
          
          mysql_select_db('db_name', $con) or die('database not selected'. mysql_error());

          Comment

          • JKing
            Recognized Expert Top Contributor
            • Jun 2007
            • 1206

            #6
            If you are developing locally using xampp or a similar product the default user is 'root' and the default password is ''.

            Comment

            • mr5zz
              New Member
              • May 2013
              • 2

              #7
              Thanks!

              Originally posted by JKing
              If you are developing locally using xampp or a similar product the default user is 'root' and the default password is ''.

              Yay! Thanks for mentioning the default user and password. I got it working now :)

              Comment

              Working...