views:

72

answers:

5
<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
echo "Connected to MySQL<br />";
?>

This is the code I am using to check if I am able to connect to Mysql.

I am having problem connecting using this code. Should I change localhost to the name of the website?

I tried ("www.abc.com","login username", "password") even this is not working.

EDIT:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'mobile4_you'@'cl79.blahblah.com' (using password: YES) in /home/mobilew4/public_html/mysql.php on line 2 Access denied for user 'mobile4_you'@'cl79.blahblah.com' (using password: YES)

A: 

Localhost refers to where the database is running. If it's running on the same server as your PHP server localhost should work fine. What kind of problems are you having? Are you sure that your username/password is correct?

jaltiere
A: 

You should replace :

  • localhost by the name of the server on which your MySQL server is
    • if your MySQL server is on the same server as PHP, then it's localhost
  • username by the login that allows you to access your database
  • password by the corresponding password

Of course, this is supposing you actually have a MySQL server up and running, with a user account created on it.

Didn't you hosting company provide you with any information related to MySQL ? Using those informations, what error message does mysql_error() give ?

Pascal MARTIN
typically, do you think that both MySQL server will be on the same server as PHP?
Karthik Kottapalli
Well, it happens, and is not a rare situation *(depends on how much you're paying, your hosting service, ... )*
Pascal MARTIN
is there any way to extract this information, since I already am logged into the cpanel?
Karthik Kottapalli
No idea about cpanel, sorry ; but I suppose those informations should be available somewhere...
Pascal MARTIN
A: 

Do you have a mysql user which you've set up? You'll need to do that. In cPanel you can do it by clicking on MySQL databases, and on phpMyAdmin you can just add a new user on the start page. Put that users' username and password in.

Arianna
A: 

You need to pay attention to the error, if any, reported by mysql_error(). This should give you some information you can use to diagnose the connection problem.

See this MySQL Forge wiki page about connection failure:

http://forge.mysql.com/wiki/Error2003-CantConnectToMySQLServer

Or this page from the MySQL Manual:

http://dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html

Bill Karwin
+1  A: 

If you're using something like cPanel, it will prefix your username and your database with your username for cpanel, for example:

> cPanel login: foo 
> Database User: bar
> Database: ey

Your database user will be foo_bar and your database will be called foo_ey

Rob
I forgot to add foo_ to the username. Thanks a lot and thanks a lot to everyone else for helping me out. I used localhost.
Karthik Kottapalli