I had little time to work on this, but here is my progress.
Our enterprise uses Database Server 1 for some things, and Database Server 2 for other things. These two servers are located in different parts of the world. Upcoming needs require Querying across both. Remembering that barriers in any enterprise are 90% human, organizational and cultural, and only 10% technical, we must live with two database servers until all parties put on their gloves and bash it out under the lights.
Meanwhile, I have a need to implement this cross-server Query as an interim solution.
Also meanwhile, I am learning about Cron jobs, and ways that I can replicate as Sxooter suggested. Any tips here would be appreciated.
Here is a rough picture:
Server1 has 10 tables on it
- Eg: Table1 = 500 records, 20 columns
- Eg: Table2 = 10000 records, 20 columns
- Eg: Table3 = 100 records, 20 columns
- For the purposes of this Query, we are only interested in looking in about 3 of the fields in any of these tables.
Server2 has 2 tables on it
- Eg: Table1 = 5000 records, 4 columns
- Eg: Table2 = 1000 records, 4 columns
- For the purposes of this Query, we are only interested in looking in about 3 of the fields in any of these tables.
The user interface will probably be a simple INPUT field that will perform a text search (maybe using REGEXP as shown) across all 12 tables (2 of which live on a different database server). We are only interested in looking for a text match on about 3 of the fields in each table.
Here is my progress:
The code segments shown below are edited (I have to protect our company secrets, you know).
I can connect to both database servers at the same time just fine.
// open database connection
$connection1 = mysql_connect($host1, $user1, $pass1) or die ("Unable to connect to: $host1 ");
$connection2 = mysql_connect($host2, $user2, $pass2) or die ("Unable to connect to: $host2 ");
// open database connection
mysql_select_db($db1, [COLOR=Blue]$connection1[/COLOR]) or die ("Unable to select database: $db1 ");
mysql_select_db($db2, [COLOR=Blue]$connection2[/COLOR]) or die ("Unable to select database: $db2 ");
First, I perform a Query on a Table that lives on Server1. Then a second Query for a Table that lives on Server2. This currently uses two separate Query statements.
// Query the FIRST database server
$query1 = "SELECT id_m AS union_id, lastname AS union_field1, firstname AS union_field2, company AS union_field3 FROM $table1 WHERE lastname REGEXP '$searchtext' OR company REGEXP '$searchtext' ";
$result1 = mysql_query($query1, [COLOR=Blue]$connection1[/COLOR]) or die ("Error in query: $query1. " . mysql_error());
// if records present
if (mysql_num_rows($result1) > 0)
{
// read each result record into an array
for($i=1; $i<=mysql_num_rows($result1); $i++)
{
$a = mysql_fetch_row($result1);
echo '<font color=red>', $a[0], ': ', $a[1], ', ', $a[2], ' from ', $a[3], '</font><br>';
}
}
echo '<font color=blue>Result1 count = ', count($a), '</font><br>';
// at this point, the $a array contains results from the FIRST database server
// ==========================================================================================================
// Query the SECOND database server
$query2 = "SELECT id_m AS union_id, lastname AS union_field1, firstname AS union_field2, company AS union_field3 FROM $table77 WHERE lastname REGEXP '$searchtext' OR company REGEXP '$searchtext' ";
$result2 = mysql_query($query2, [COLOR=Blue]$connection2[/COLOR]) or die ("Error in query: $query2. " . mysql_error());
// if records present
if (mysql_num_rows($result2) > 0)
{
// read each result record into an array
for($i=1; $i<=mysql_num_rows($result2); $i++)
{
$b = mysql_fetch_row($result2);
echo '<font color=red>', $b[0], ': ', $b[1], ', ', $b[2], ' from ', $b[3], '</font><br>';
}
}
echo '<font color=blue>Result2 count = ', count($b), '</font><br>';
// at this point, the $b array contains results from the SECOND database server
The I do an “array_merge” statement. Then a “asort” statement to re-sort the newly combined results from Server1 and Server2 into one single, happy array.
$c = array_merge($a, $b);
echo '<font color=blue>Merged count = ', count($c), '</font><br>';
asort($c, union_id); // does this actually sort on the union_id column?
I also "echo" (as seen in the code segments above) certain things to monitor my progress.
From looking at my echo statements, the REGEXP properly returns records that match the text-search criteria.
Eg: 14 results from Server1, and 7 results from Server2.
Results from Server1 seem to be properly in the 4-column, x-row Array called $a.
Results from Server2 seem to be properly in the 4-column, n-row Array called $b.
By the way, the overall response time seems acceptable for now.
But today, due to lack of stimulation, I seem to get lost.
I’m not 100% sure about the way I merge and re-sort the Arrays. Am I doing it correctly? You would probably think something like this would be obvious for someone posting a message in this Forum, wouldn’t you?
I did indeed look at the info on php.net about “array_merge” and “asort”, and was able to at least make some progress.
But for some reason I can not seem to properly handle the contents of my merged, re-sorted Array $c. I’m sure it’s straightforward, and I’m just overlooking something.
So, I need to validate that my “merge” and “re-sort” actually work.
Then I would like to display the contents of the resulting Array.
Can anyone give tips on how to handle my post-merge and post-sorted Array?
Meanwhile… yes… I will work on replicating or relocating all this to one database server. I'll also continue learning how to manipulate Arrays.
Does this approach make any sense? Any tips?
Before I close,
// ====== Close database connection
mysql_close($connection1);
mysql_close($connection2);
... I will just say that once all of this works, this little code segment might be a handy thing to keep in our back pocket for future use.