0

Here is the code I use for connecting to a mysql database on localhost (xampp on win 7) and on remote server.

Everything works fine, but now I need to create a connection from localhost to a remote database so I can change remote tables by working on local files.

I'm not sure where to start, but suppose the existing code is a start point.

Any help?

$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

if (strpos($url, 'localhost') == true) {
    define('DBHOST','localhost');
    define('DBNAME','flash');
    define('DBUSER','root');
    define('DBPASS','');
}

else {
    define('DBHOST','...');
    define('DBNAME','...');
    define('DBUSER','...');
    define('DBPASS','...');
}

try {
    $db = new PDO("mysql:host=".DBHOST."; dbname=".DBNAME, DBUSER, DBPASS);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch (PDOException $e) {
    echo $e->getMessage();
    exit;
}

$db->query('SET character_set_client = "utf8"');
$db->query('SET character_set_results = "utf8"');
$db->query('SET collation_connection = "utf8_general_ci"');

error_reporting(E_ALL);
ini_set('display_errors', 'on');
Dharman
  • 30,962
  • 25
  • 85
  • 135
qadenza
  • 9,025
  • 18
  • 73
  • 126
  • 1
    Simply changing the DBHOST value to the remote server's IP address (ex: 192.168.2.1) could do it. You may need to configure the remote database to be available remotely. This is done in quite a lot of ways though. – user1453870 Jan 21 '20 at 21:55
  • @user1453870, I tried to find any option on remote database to configure remote access - without success, didn't find any simmilar option. – qadenza Jan 21 '20 at 22:02
  • You will need to open up the relevant ports in your server firewall on the remote server and the [remote MySQL](https://stackoverflow.com/questions/14779104/how-to-allow-remote-connection-to-mysql) – Martin Jan 21 '20 at 22:19
  • You should change `$url` to `$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];` – Amanjot Kaur Jan 22 '20 at 06:34

0 Answers0