Connecting a PHP page and a MYSQL database

dbprofil

Registered
Aug 7, 2006
2
0
151
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
 

Gareth

Well-Known Member
Feb 11, 2004
71
0
156
Isle of Anglesey, UK
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
 

dbprofil

Registered
Aug 7, 2006
2
0
151
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
 

WebScHoLaR

Well-Known Member
Dec 14, 2005
508
3
168
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.

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_.
 

SageBrian

Well-Known Member
Jun 1, 2002
413
2
318
NY/CT (US)
cPanel Access Level
Root Administrator
dbprofil said:
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.
 

astar

Member
Jun 15, 2006
7
0
151
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?
 

Spiral

BANNED
Jun 24, 2005
2,018
8
193
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.