The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Access denied for user

Discussion in 'General Discussion' started by razorbackrob, Feb 28, 2010.

  1. razorbackrob

    razorbackrob Registered

    Joined:
    Feb 28, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    I'm trying to write PHP code to query my database, but even though I have assigned all privileges to my user id, when I try to select the database I get the error "Access denied for user 'myuser'@'localhost' to database 'mydatabase'. The initial connection works fine.
    $con = mysql_connect ("localhost", "myuser", "mypassword");
    if (!$con)
    {
    die('Could not connect: ' .mysql_error());
    } else echo '<p>Connection succeeded!</p>';

    $db = mysql_select_db('mydatabase', $con);
    if (!$db)
    {
    die('Can not connect to datamodel: ' . mysql_error());
    }
    The aptHost MySQL Account Maintenance screen shows my user as having all privileges to my database. Any help would be appreciated!
     
  2. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    In a cPanel/WHM environment, database names and database user's usernames are prefixed by the cPanel username and _.

    So if your cPanel username is example, and you created a database user: bob, you need to use the following username in your code: example_bob -- if your database name is db, then you need to use the following database username in your code: example_db
     
  3. razorbackrob

    razorbackrob Registered

    Joined:
    Feb 28, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Thanks so much, that fixed it!
     
Loading...

Share This Page