Two MySQL servers, one script

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am attempting to put together one script that pulls data from one
database on its own server, and data from another database on its own
server, which is off-site.

Server 1 - Only allows shared memory access to the database. Also
disallows http:// includes. This is where the database of names is
stored. This is our internal MySQL server.

Server 2 - Only allows shared memory access to the database. So
anything that calls the connection has to be on the same server. This
is our external web server maintained by an outside party.

To get a list of names, I created a PHP page on Server 2 that simply
includes a script from Server 1. The script on Server 1 connects to
the database, runs the query and prints the names to the screen. I did
this, because we wanted the web page on our web server which is Server
2, but since the the MySQL on Server 1 only accepts shared memory
access, the script that called it had to be on the same server.

Now what I want to do is include the ages of each person. That is
information stored in a MySQL database on Server 2, the web server. I
can't modify the Script on Server 1 to include the database connection
on Server 2 since Server 2 also requires shared memory access. So I
tried putting an include file on Server 2 and calling it from Server
1, but Server 2 doesn't allow http:// includes.

Please keep in mind that I have had problems with the include. You
would think that being an include file, it would have access to all
the data on the page that called it. I have found this not to be the
case. The include script that I called, did not recognize variables
that were declared in the file that was calling it. Perhaps because it
was on another server? The only solution I could find was to pass the
variables to the include file through URL. So if I were to put another
include statement on the original page on Server 2, the off site
include doesn't have access to that database connection. Any help is

Re: Two MySQL servers, one script

Quoted text here. Click to load it

Tried moving all code from the include to the main file. All I left on
the include file was a connection to the database. Unfortunately, the
main file doesn't seem to be able to recognize the database connection
from the include file. The query only works when it is in the include

Re: Two MySQL servers, one script

Quoted text here. Click to load it


Hmmms, it seems there's an epedemic of people including by HTTP (search  
the recent archive of this group). While you can include the _output_ of  
an php file by HTTP, a 'real' include, which will recognize your variables  
can only be done using either the local filesystem or by including raw PHP  
code from elsewhere instead of the result.

The fact that these 2 servers can hardly communicate is a real problem.  
You'll either have to get the output data and link it to age 'manually'  
with PHP, or you'll have to really think about the database(server) setup.  
Preferably all related data on one server, possibly a master/slave  
Rik Wasmus

Re: Two MySQL servers, one script

Quoted text here. Click to load it

We have discussed changing the setup, but are limited in what we are
allowed to do and by how much time we could dedicate to a project.
Which is practically none. (If they would let us shut down the IT
department for a month or so, buy at least one heavy duty server,
merge everything on to it, and adjust the databases to be more
cohesive(15 years of various workers results in a hodge podge of
databases/tables), that would be great. But it isn't going to happen.)

You mention including raw PHP. I don't know what you mean by that.
Could you please describe the process vaguely, so I know what to
search for?

Re: Two MySQL servers, one script

Quoted text here. Click to load it

Then you'll have to have a PHP script running on one server, get the  =

output of that on the other (using CSV or serialize() for the output com=
es  =

to mind), and link the related data on that server with it with a bunch =
of  =


Quoted text here. Click to load it

On a typical HTTP request, PHP code will be executed, and all you get is=

the output. If you however output the code itself (either by disabling P=
HP  =

(for that directory), giving it another non-processed extention, or by  =

'building PHP with PHP' (<?php echo '<?php echo "foo"; ?>' ?>)), it can =
be  =

processed on the requesting server.

Quoted text here. Click to load it

It wouldn't help you, as the code doesn't get executed on the server it =

comes from in that case, but on the server doing the include.  Think abo=
ut  =

it, if I could just include from other servers by HTTP and have access t=
o  =

their code, I could do something like this:

include(' ');
mysql_query("UPDATE `balance` SET `amount` = 999999999999 WHERE `id` =


So, clearly it doesn't work that way.
-- =

Rik Wasmus

Re: Two MySQL servers, one script

Quoted text here. Click to load it

Just read where someone with a similar problem suggested renaming the
include file to .txt. Is that what you mean by raw PHP? This might
work, as the file that is being included, itself calls an include that
connects to the database. So even if I were to read the include file
as raw text, no one should be able to see the database connection

Re: Two MySQL servers, one script

Quoted text here. Click to load it



Which would be non-existant on the server it executes on. PHP-code serve=
d  =

that way is plain text, NOTHING of the envirnoment on the other server  =

will be accessable.

Quoted text here. Click to load it

Clearly not.

TO give you an illustration of the terrible hoops you have to jump throu=

Server 1 (people.php):
$q = mysql_query('SELECT id, name FROM people');
$return = array();
while($row = mysql_fetch_assoc($q)) $return[] = $row;
echo serialize($return);

Server 2:
$people = unserialize(file_get_contents('http://server1/people.php'));=

//includes would be tedious:
// ob_start();
// include('http://server1/people.php');
// $people = unserialize(ob_get_clean());
if(is_array($people) && !empty($people)){
    foreach($people as $person){
        $bday = mysql_query('SELECT `birthday` FROM `birthdays` WHERE id =

        $date = $bday ? mysql_result($bday,0,'birthday') : 'unknown';
        echo " <br>";

Which might be somewhat streamlined by fetching all the birthdays in one=

query to an array indexed by 'id', and linking to it in the foreach loop=

of $persons, it's still terribly inconvenient.
-- =

Rik Wasmus

Re: Two MySQL servers, one script

Quoted text here. Click to load it

It seems that I am getting somewhere thanks to your help. I am not
getting any errors. Still not quite there yet. Here is roughly what I

File to be included:
$query= "SELECT name
FROM `names` name";
$result = mysql_query($query, $database) or die('Query failed: ' .
$return = array();
while($row = mysql_fetch_assoc($result)) $return[] = $row;
echo serialize($return);

Which I take it, should return an array of $return[]. The file that
calls the include has:
 $name= unserialize(file_get_contents('https//

Two things I need to do here, are 1) see if the array returned any
data and 2) print the names to screen.

For the first problem I am guessing I can just check to see if the
array, $return[], is empty. (Seem to recall some isempty() function)

For the second problem, I am wondering if this would work:
while ($row = mysql_fetch_array($name)) {

Or should loop through with a variable such as:

while (x<=$num_rows){

Re: Two MySQL servers, one script

Wondering if using functions wouldn't be a better way to go. Include
the file form the distant server, and that include file contains a
function that returns what I need. Not sure if that will work in this

Re: Two MySQL servers, one script

Quoted text here. Click to load it

Nope, it doesn't recognize the function.

Re: Two MySQL servers, one script

Quoted text here. Click to load it

Found a suggestion that mentioned changing the included file to .txt,
which should read it in as raw PHP. I tried that. The problem is that
the file being called for inclusion itself calls another file on the
same server as itself. This file it calls is the database connection
info. By including the file on a seperate server, the include file can
no longer access the needed database connection.

Re: Two MySQL servers, one script

Quoted text here. Click to load it

Ain't going to work.  Remote include() only includes the output of a
script, not the executable code.

What you need is to deploy a Web service provider on Server 2 and
consume the service from Server 1.  Ideally, though, you should allow
access to Server 2 databases from Server 1 (preferably via SSL).


Re: Two MySQL servers, one script

Quoted text here. Click to load it

  True, only if the file is parseable by PHP. Something, like
foo.php.txt will definitely be a trouble.

  <?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com    Blog:

Site Timeline