简体   繁体   中英

MySQL aunthentication fails on php but not in phpmyadmin

I am developing a PHP website with MySQL access. My hosting site is shared and I created a DB from the phpmyadmin that is installed on the shared hosting server. I created 2 users, one with administrative privileges, the other having only read/write privileges. I can connect with phpmyadmin using both accounts successfully but when I tried to access mysql using the same accounts and using the same credentials, php gives me a:

Database connection failed: Access denied for user 'myusername'@'localhost' (using password: YES)

Which is very weird. PHPMyAdmin says that the server is localhost so that means I am using the correct server parameter to my mysql_connect function in php.

Does anyone have any idea what went wrong? Could it be possible that mysql is only enabled for PHPMyAdmin (which is absurd)?

Thank you very much and I'll appreciate the help. :)

Here is a sample code (username and password are replaced for security purpose):

defined('DB_SERVER') ? null : define("DB_SERVER", "localhost");
defined('DB_USER')   ? null : define("DB_USER", "host_admin");
defined('DB_PASS')   ? null : define("DB_PASS", "mysuperpassword");
defined('DB_NAME')   ? null : define("DB_NAME", "mydb_inhostingserver");

...

$this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);

if (!$this->connection) {
    die("Database connection failed: " . mysql_error());
} else {
    $db_select = mysql_select_db(DB_NAME, $this->connection);

    if (!$db_select) {
    die("Database selection failed: " . mysql_error());
    }
}

...

Database connection failed: Access denied for user 'host_admin'@'localhost' (using password: YES)

I also echoed the 4 constants and their values are correct! I'm really lost here...

Additional Information:

  • The FTP address is: ftp.thewebsiteimtalkingabout.com
  • The PhpMyAdmin address is: mysql.thewebsiteimtalkingabout.com

So would this mean that they my code and MySQL's phpmnyadmin is running on the same host?

Answer:

Ok so I was testing the theory that they are in a different server although their domain's are the same. I extracted their respective ipaddress and found that they are different. I then replaced the 'localhost' parameter to the ipaddress of where the phpmyadmin resides then boom! It's now connected. Thank you very much for the replies everyone!

Eepot - that doesnt mean that your application sits on the same server as the mySQL database.

For instance, on my host, the database it on another server - but phpmyadmin is ALSO on THAT server. This means that "phpmyadmin" is reporting the server as "localhost" - but my application needs to connect to the server using the full server name.

You need to ask your hosting provider for the details to connect to your mySQL server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM