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.

Connecting a PHP page and a MYSQL database

Discussion in 'Database Discussions' started by dbprofil, Aug 7, 2006.

  1. dbprofil

    dbprofil Registered

    Joined:
    Aug 7, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Hi

    I am having trouble connecting a php page to a database.

    I have gone into cpanel and created a database:
    current databases: dbprofil_
    current users: dbprofil_dbprofi
    **I have not added anything to 'Access Hosts' it just says 'local host'**


    All i want to do is connect to the database using my php page:


    $dbConn = mysql_connect ("localhost", "dbprofil_dbprofi", "*****") or die ('MySQL connect failed. ' . mysql_error());
    mysql_select_db("dbprofil_") or die('Cannot select database. ' . mysql_error());


    ...but it wont connect i get the error:


    Cannot select database. Access denied for user: 'dbprofil_dbprofi@localhost' to database 'dbprofil_'

    My website is hosted by www.hostingdepartment.net

    any help you could give me would be greatly appreciated.

    many thanks
    Bil
     
  2. Gareth

    Gareth Well-Known Member

    Joined:
    Feb 11, 2004
    Messages:
    71
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Isle of Anglesey, UK
    This should be in the format: account username_database name

    So if your account username is dbprofil, and the database name you created is dbprofil, then you will need to set the database name in your php files as

     
  3. dbprofil

    dbprofil Registered

    Joined:
    Aug 7, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    changes made

    Hi Gareth

    I've made some changes as calling then both the same name was a little confussing.

    the database is still called 'dbprofil_'
    but the username is now just 'dan'
    In cpanel the current user has changed to 'dbprofil_dan'

    so the php is:

    $dbConn = mysql_connect ("localhost", "dan_dbprofil", "****") or die ('MySQL connect failed. ' . mysql_error());
    mysql_select_db("dbprofil_") or die('Cannot select database. ' . mysql_error());

    i still get an error saying:


    Warning: mysql_connect(): Access denied for user: 'dan_dbprofil@localhost' (Using password: YES) in /home/dbprofil/public_html/plaincart/library/database.php on line 4
    MySQL connect failed. Access denied for user: 'dan_dbprofil@localhost' (Using password: YES)

    thank you for you help

    Bil
     
  4. WebScHoLaR

    WebScHoLaR Well-Known Member

    Joined:
    Dec 14, 2005
    Messages:
    511
    Likes Received:
    2
    Trophy Points:
    18
    Location:
    Planet Earth
    You are still using the incorrect credentials. Your cPanel username is dbprofil and the database user that you have created is dan but as cPanel appends the cPanel username with the database and the datbase user so you have to use dbprofil_dan as the database user where currently you are using dan_dbprofil as the database user that is resulting in following error.

    Similarly with the database.dbprofil_ is the prefix that will be automatically appended to the database name so you have to use the compelte name i.e dbprofil_DATABASENAME and not just dbprofil_.
     
  5. SageBrian

    SageBrian Well-Known Member

    Joined:
    Jun 1, 2002
    Messages:
    415
    Likes Received:
    2
    Trophy Points:
    18
    Location:
    NY/CT (US)
    cPanel Access Level:
    Root Administrator

    So, if, uh, the username is dbprofil_dan, then why are you getting the error for dan_dbprofil ? :)

    I'd check spelling.
     
  6. astar

    astar Member

    Joined:
    Jun 15, 2006
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    mine got an error of:
    Warning: mysql_connect(): Lost connection to MySQL server during query in /home/username/public_html/directory/class/db.php on line 28
    Error connecting to the serverLost connection to MySQL server during query

    the database are correctly created from the remote server.
    Anybody got an idea?
     
  7. Spiral

    Spiral BANNED

    Joined:
    Jun 24, 2005
    Messages:
    2,023
    Likes Received:
    7
    Trophy Points:
    0
    Oh good grief ....

    Go read what Gareth said a little closer !!!!

    In a Cpanel based server, the MySQL usernames and database names
    are both ALWAYS prefixed by the web hosting account username.

    If your web hosting account login is "pizza" then all of your MySQL databases
    will begin with "pizza_" and all your MySQL usernames will begin with "pizza_"

    If I were to create a database called "pizza" in Cpanel using the above example
    then my database would actually be called "pizza_pizza" because the hosting login
    always prefixes the database name!

    Likewise if I named the database "pepperoni" then the database name to use
    in your programs would actually be "pizza_pepperoni".

    The MySQL usernames work exactly the same as the database names in
    regard to the web hosting username being prefixed to the MySQL username.
    If I created a MySQL user called "user" then it would be "pizza_user" in
    my web site scripts and programs.

    Now with that said ....

    When you create a new MySQL user, that new user DOES NOT have access to
    the database you created until you specifically grant access for the new MySQL
    user to be able to access the new database. Don't forget this step else your
    login won't work even if you do type the correct database name and username!

    Basically, you have a 3 step process ....

    1. Create the database

    2. Create the user for the database

    3. Give the new user access to the database


    All three of these functions appear on Cpanel's database management screen
    and assuming you have done those three steps correctly, you should be able
    to use the database with your scripts.
     
Loading...

Share This Page