Jump to content

[SOLVED] cannot connect to MySql database


pascalc

Recommended Posts

I'm new to php and MySql, and recently starting using phpMyAdmin on Yahoo.  I created a new database (TestDB) and a 'login_table' with only 3 columns - loginID, password, and emal_address, and inserted 1 row.  I inserted a new user in the user table on the MySql database for my php scripts - 'pcscript', and gave it select, insert, update, and delete privileges to TestDB.  I use the following simple code to try to connect to the database:

 

<?

 

$con = mysql_connect("mysql", "pcscript", "password123");

 

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

@mysql_select_db("TestDB") or die("Cannot select DB!");

 

mysql_close($con);

 

?>

 

I get the following error message: "Could not connect: Access denied for user 'pcscript'@'localhost' (using password: YES)".  Even though the user 'pcscript' was created under the MySql host, but the error refers to 'pcscript@localhost'.  Why can't I connect?  Very frustrated.  Please help.

 

Pascal

Link to comment
Share on other sites

some server setups have prefixes added to user names and database names.  A common setup is for instance, if you owned www.mycoolsite.com and your assigned account name is mycoolsi that could be the prefix followed by an underscore followed by your username: mycoolsi_pcscript.  same with the database name: mycoolsi_testdb. 

 

You want to talk to your host or read their documention to find out if they add some kind of prefix.

Link to comment
Share on other sites

Also do bear in mind that a lot of shared, re-seller and virtual dedicated hosting plans have prefixes of usually the username which it has been registered with, as Crayon Violent said, however with prefixes, database names you enter tend to get cut off. This has often been my problem in the past, I've known about the database prefixes, however I've sometimes assumed that the whole name I entered would be used which wasn't the case.

 

On a side note, most web hosting services provide local MySQL servers. And therefore you would need to enter "localhost" for the server address.

 

e.g.

 

$con = mysql_connect("localhost", "pcscript", "password123");

Link to comment
Share on other sites

Thanks for the response, but it wasn't the prefixes.  I just deleted the user, and inserted him again with 'localhost' as the host, even though the script refers to 'MySql' as the host.  That was confusing to me.  It's working now.  Thanks.

Pascal

 

I have a feeling I'll be back soon...

Link to comment
Share on other sites

On a side note, most web hosting services provide local MySQL servers. And therefore you would need to enter "localhost" for the server address.

 

I almost mentioned that but then I just figured he put something generic in there like most people do with host/name/pw when showing code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.