从php中的localhost连接到在线数据库

时间:2015-05-17 15:09:33

标签: php connection

我有两个数据库

其中一个在本地服务器上,另一个在线。

我想通过PHP代码在localhost中连接它们。

如何为此目的定义此参数?本地服务器是否需要特殊配置?

//database that is local
    define( "HOST", 'localhost' ) ;
    define( "DBUSER", 'root' ) ; 
    define( "DBNAME", 'name_db' ) ;  
    define( "DBPASS", '' ); 


      //database that is online
        define( "HOSTPM", 'localhost' ) ;
    define( "DBUSERPM", 'username' ) ; 
    define( "DBNAMEPM", 'name2_db' ) ; 
    define( "DBPASSPM", 'password' );  

$db = new  PDO('mysql:host='. HOST .';dbname='. DBNAME . ';charset=utf8', DBUSER, DBPASS);
        $dbpm = new  PDO('mysql:host='. HOSTPM .';dbname='. DBNAMEPM . ';charset=utf8', DBUSERPM, DBPASSPM);
        if (!$db) {
                die('Could not connect: ' . mysqli_error());
            }
        if (!$dbpm) {
           die('Could not connect: ' . mysqli_error());
       }

1 个答案:

答案 0 :(得分:2)

默认情况下,您无法远程访问mysql服务器

检查修改权限的答案:connect to mysql server remotely

  

GRANT ALL ON ON数据库。* TO user @ ipaddress IDENTIFIED BY'password';

您应该使用以下方法强制重新加载授权表:

  

FLUSH PRIVILEGES;