PHP Developers Network
http://forums.devnetwork.net/

Connecting to two DBs on two separate hosts - how?
http://forums.devnetwork.net/viewtopic.php?f=2&t=142427
Page 1 of 1

Author:  simonmlewis [ Mon Apr 04, 2016 7:23 am ]
Post subject:  Connecting to two DBs on two separate hosts - how?

I need to run a query every few hours that checks from our USA server, that the UK database has certain credentials in the "rcstock" field.

So I need to open two connections: 1, to local USA, and 2, to UK on a different host server.

USA is allowed in, so that should be fine. But I am getting a while screen. No errors.
There is nothing in the error log for it.

Am I doing it right??

Syntax: [ Download ] [ Hide ]
<?php
$dbuk = mysql_connect('host','username','pass')or die ('Could not connect to website.co.uk');
$dbus = mysql_connect('localhost','username','pass')or die ('Could not connect to website.com');
mysql_select_db('databasename', $dbuk) or die ('Could not connect to website.co.uk Database');
mysql_select_db('databasename', $dbus)or die ('Could not connect to website.com Database');


        $result = mysql_query ("SELECT id, romancode, title FROM products WHERE pause <> 'on' AND romancart = 'uk'", $dbus);
        while ($row = mysql_fetch_object($result))
          {
          $romancode = substr($row->romancode, 2); // "removes us from romancode for uk stock check"
          echo "$row->title - $row->romancode ";
         
          $resultuk = mysql_query ("SELECT rcstock FROM products WHERE romancode = '$romancode'", $dbuk);
          while ($rowuk = mysql_fetch_object($resultuk))
          {
            if ($rowuk->rcstock == "available")
            {
            mysql_query ("UPDATE products SET rcstockuk = 'available' WHERE id = '$row->id'", $dbus);
            echo " available<br/>";
            }
            else if($rowuk->rcstock == "out of stock")
            {
            mysql_query ("UPDATE products SET rcstockuk = 'out of stock' WHERE id = '$row->id'", $dbus);
            echo " out of stock<br/>";
            }
            else if($rowuk->rcstock == "in stock")
            {
            mysql_query ("UPDATE products SET rcstockuk = 'in stock' WHERE id = '$row->id'", $dbus);
            echo " in stock<br/>";
            }
          }mysql_free_result($resultuk);
          } mysql_free_result($result);
?>

Author:  requinix [ Mon Apr 04, 2016 7:56 am ]
Post subject:  Re: Connecting to two DBs on two separate hosts - how?

Is the connection timing out? The code looks fine so...

Author:  Christopher [ Mon Apr 04, 2016 10:05 pm ]
Post subject:  Re: Connecting to two DBs on two separate hosts - how?

Does the UK server allow remote connections? Does it require SSL? Do you have the right server name, port, username and password?

PS - do not use the mysql_* library -- it is no longer maintained. Upgrade to mysqli or PDO.

Author:  Celauran [ Tue Apr 05, 2016 6:24 am ]
Post subject:  Re: Connecting to two DBs on two separate hosts - how?


Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/