Help - Search - Member List - Calendar
Full Version: Can't connect to mySQL database thru php
WorkTheWeb Forums > Web Hosting Help > MySQL Help
Support our Sponsors!
earthrisers
I have created a mySQL database (my first), and successfully tested it within phpMyAdmin. Now I'm trying to connect to it through a php program, and it's not working. I get this message:

QUOTE
Could not select the database: Access denied for user: 'mydbusername' to database 'mydatabasename'


(The actual message, of course, contains my actual database user name and database name.)
Here is the code I'm using. The values I enter in each of the variables exactly matches the values that appear in phpMyAdmin for my database. The code below has "dummy" values, except for the value 'localhost', which is what is actually in my code.

CODE

DEFINE ('DB_USER', 'mydbusername);
DEFINE ('DB_PASSWORD', 'mypassword');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'mydatabasename');

// Make the connnection and then select the database.
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );
mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );


...what am I missing, that keeps me from being able to get to my database?
phpfreak
Hi,

Usually, I wouldn't define constants for this, I would just do a variable, however it's irrelevant to the problem.

From what, I see, you're missing the MySQL resource identifier in your mysql_select_db function.

Try altering the line:

CODE
mysql_select_db (DB_NAME, $dbc) OR die ('Could not select the database: ' . mysql_error() );


And see if that helps. Notice, I added the $dbc (your connection identifier) as the second argument to the mysql_select_db function.

Try that and see if it helps!

Sincerely,

earthrisers
Thanks for the suggestion...

Tried it again twice, once simply adding the second argument as you suggested, and then again by also using constants instead of variables in the call.

No go; both times I get the same result as before.
Apparently I connect to mySQL okay, but am denied access to my database.

I also tried two variations of my dbusername: once with the username preceded by my domain account name ('accountname_dbusername'), and once without the 'accountname_' prefix. Same result - the error message mirrors back the name I attempt, and says access is denied.

Ernie
phpfreak
In cPanel, below the section where you create MySQL databases and users, did you grant permissions to this database for that new user you created?
phpfreak
Hi,

I took care of it for you. Try accessing your database now.

For future reference, when you create a user and a database, you also have to assign permissions to access the database.

You'll see the first section on the MySQL page where it says:

User <select menu> DB: <select menu>
Privilages <checkboxes>

Add User to DB

Just complete that form and you should be all set!
earthrisers
I'm pretty sure I had assigned privileges... I left the "ALL" checkbox checked, right after the word Privileges, when I created the user.

In any case, I have now copied&pasted the php code that was displayed by phpMyAdmin, and I think I'm successfully connected...

not sure what was different, from the code I originally entered... couldn't see a difference at first glance, but I'll go look more closely now.

Thanks again,

Ernie
phpfreak
Gremlins cool.gif
dtaberner
QUOTE(phpfreak @ Mar 8 2005, 12:46 AM)
Gremlins  cool.gif
*



Of the human type ? wink.gif

Caveman
earthrisers
Ah, something else I notice now that wasn't "intuitively obvious" in phpMyAdmin:

YOu have to "add user" twice --- first, to create the user, in the panel underneath the "main" database maintenance panel, and then, INSIDE the main maintenance panel, you have to ADD that newly created user. The second "add user" seems to be the step that actually assigns privileges...

Ernie

PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.