duancuan7057
duancuan7057
2011-03-30 21:47

在远程服务器上的MySql数据库上连接时出现问题

已采纳

I have been developing my php mysql application locally and it worked fine with this connection parameters:

    // Database connection params
define('HOST', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DATABASE', 'some_db');

at some point I wanted to put application online with connection parameters:

define('HOST', 'somedomain.com');
define('USERNAME', 'some_db_admin');
define('PASSWORD', 'Password1!');
define('DATABASE', 'some_db');

When I try to connect to remote database like this:

function db_connect()
{
    $connection = @mysql_connect(HOST, USERNAME, PASSWORD); 
    if(!$connection)
    {
        return false;
    }

    if(!mysql_select_db(DATABASE))
    {
        return false;
    }

    return $connection;
}

I get error: Access denied for user 'some_db'@'localhost' (using password: NO)

How can it possibly be localhost?

I have regularly created database and user that database using using database wizard in cpanel.

What can be the problem?

Thanks a lot!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongtu0363 dongtu0363 10年前

    If your PHP code is at the same host where the MySQL database is then setting HOST to localhost should solve your problem

    define('HOST', 'localhost');

    点赞 评论 复制链接分享
  • doudongdang4483 doudongdang4483 10年前

    It also seems that you swapped your constants, because some_db is not likely to be a user but rather a database. Try debugging and output the constants' values just before the 'mysql_connect' call. If this does not work, try debug_backtrace().

    点赞 评论 复制链接分享

为你推荐