Community Forums
Connect with us on LinkedIn
+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    2

    Default Connecting a PHP page and a MYSQL database

    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. #2
    Member Gareth's Avatar
    Join Date
    Feb 2004
    Location
    Isle of Anglesey, UK
    Posts
    73

    Default

    current databases: dbprofil_
    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

    dbprofil_dbprofil
    HostOrca - hostorca.co.uk
    UK and US Reliable Shared Hosting Solutions
    Where Customer Service Counts!

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    2

    Default 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. #4
    Member WebScHoLaR's Avatar
    Join Date
    Dec 2005
    Location
    Planet Earth
    Posts
    506

    Default

    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.

    Warning: mysql_connect(): Access denied for user: 'dan_dbprofil@localhost' (Using password: YES)
    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_.

    Regards,
    WebScHoLaR

    WebHosting Blog: http://WebScHoLaR.net

  5. #5
    Member SageBrian's Avatar
    Join Date
    Jun 2002
    Location
    NY/CT (US)
    Posts
    386

    Default

    Quote Originally Posted by dbprofil
    Hi Gareth

    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());


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

    I'd check spelling.

  6. #6
    Member
    Join Date
    Jun 2006
    Posts
    7

    Default

    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. #7
    BANNED
    Join Date
    Jun 2005
    Location
    Wild Wild West
    Posts
    2,025

    Default

    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.

Similar Threads & Tags
Similar threads

  1. Replies: 2
    Last Post: 03-22-2011, 12:54 AM
  2. PHP, MySQL Database Connection Problem
    By pl0tted in forum Database Discussions
    Replies: 1
    Last Post: 01-18-2009, 01:12 PM
  3. problem connecting to MySQL database
    By Kailash1 in forum cPanel and WHM Discussions
    Replies: 1
    Last Post: 07-23-2007, 06:03 AM
  4. Why can't I see the graphics in cpanel 8.55 mysql database setup page?
    By Bensonn in forum cPanel and WHM Discussions
    Replies: 0
    Last Post: 01-15-2004, 03:18 PM
  5. Connecting to Database
    By sbrad in forum cPanel and WHM Discussions
    Replies: 11
    Last Post: 09-30-2002, 03:20 AM
Linkedin       Facebook       Twitter       RSS       Flickr       YouTube