0

I am begginer and I always have this problem when I am going to move my website from my computer to host or server, some codes can't run or I always see some errors.

I created a database and I set a privilege(username and password) but I have error yet.

This is the Error:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'mypassword'@'localhost' (using password: YES) in /home/qmelkir/public_html/include/connect.php on line 5 Access denied for user 'mypassword'@'localhost' (using password: YES)

and this is mysql_connect order:

$con=mysql_connect('localhost',$password,$username) or die(mysql_error());

How to solve it?

and How to work on localhost to don't have problem with host and server?

thanks

  • 3
    "localhost" is the machine where you're running the code. For that to work, you need to a) have mysql running there b) have the right user/pass credentials, so make sure both of those are true (which is not the case right now) – Mike 'Pomax' Kamermans Sep 24 '14 at 15:57
  • 3
    the order is host, user, password see http://php.net/mysql_connect – Le_Morri Sep 24 '14 at 15:57
  • 1
    Please, [don't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). *They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation)*. See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – Jay Blanchard Sep 24 '14 at 15:59

2 Answers2

1

So to make it an answer:

The correct order for the params is. see here

$host, $username, $password

so i guess you mixed it up.

and make sure that your server is listening on localhost mine for exapmle is listening to mysql.mydomain.com

and as @Jay Blanchard pointed out the mysql_* functions are deprecated. so better don't use them.

Le_Morri
  • 1,619
  • 14
  • 18
0

"localhost" is the machine where you're running the code. For that to work, you need to

  1. you are using the correct argument ordering as per http://ca2.php.net/manual/en/function.mysql-connect.php
  2. you have MySQL running on the same machine this code runs and
  3. you are using a username and password combination that has connection permissions.

Not all of these are the case right now.

As an additional note, the mysql_... functions are considered dangerous and have been replaced with mysqli_... equivalents (note the i in there), which have a different argument ordering (so you can't accidentally use mysql_... functions as a typo by forgetting that i) and should be used instead. Especially if your host has an up to date PHP version, the msql_... function calls will plain not work and cause errors that wouldn't solve by shuffling arguments around.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
  • The problem is almost certainly that he reversed $username and $password as per @Le_Morri's comment. – Eric J. Sep 24 '14 at 16:00
  • indeed, and he'd find that out by running through step 1. We've all seen too many questions where even after step 1, it turns out steps 2 and 3 are still necessary though, so adding them to the answer just in case is cheap and effective. – Mike 'Pomax' Kamermans Sep 24 '14 at 16:02