sahar21
sahar21

Reputation: 186

connect to online database from localhost in php

I have two databases

One of them is on local server and the other is online.

I want to connect both in localhost by PHP code.

How to define this prameters for this purpose? Is special configuration required on local server?

//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());
       }

Upvotes: 1

Views: 9227

Answers (1)

Ramy Sabry
Ramy Sabry

Reputation: 368

By default you don`t have access to mysql server remotely

Check that answer to modify privileges : connect to mysql server remotely

GRANT ALL ON database.* TO user@ipaddress IDENTIFIED BY 'password';

You should force a reload of the grant tables using:

FLUSH PRIVILEGES;

Upvotes: 3

Related Questions