简体   繁体   中英

How to Combine 3 database tables form 3 different databases into Database 4

I am trying to create a separate search site ( site4 using database4 ) that is updated every hour from 3 different websites each having their own database. I want to combine the data from database1, database2, and database3 into database4.

I also want to remove the duplicates during the combining, so I was told to use the MySQL UNION function.

On the same server I have 4 individual websites with each site having their own MySQL database:

site1 --> database1, 1 table, 16 fields

site2 --> database2, 1 table, 16 fields

site3 --> database3, 1 table, 16 fields

site4 --> database4, 1 table, 16 fields (currently empty)

All 4 databases have an identical structure where as each database has only 1 table with 16 fields.

In all 4 databases, the table names are the same (Post_Data) and all 16 fields are identical. The index field (field 11) is named Post_Date.

With some forum help I was able to write the following PHP code, but it does not work and I do not get errors.

Can you see what is wrong in the code below and what needs to be done to fix it?

Thanks in advance.

<?php

   // Website 1
      $host1 = 'site1.com';
      $database1 = 'data_1';
      $username1 = 'user_1';
      $password1 = 'pass_1';
      $TableName1 = 'Post_Data';

   // Website 2
      $host2 = 'site2.com';
      $database2 = 'data_2';
      $username2 = 'user_2';
      $password2 = 'pass_2';
      $TableName2 = 'Post_Data';

   // Website 3
      $host3 = 'site3.com';
      $database3 = 'data_3';
      $username3 = 'user_3';
      $password3 = 'pass_3';
      $TableName3 = 'Post_Data';

   // Website 4 - Search Database
      $host4 = 'site4.com';
      $database4 = 'data_4';
      $username4 = 'user_4';
      $password4 = 'pass_4';
      $TableName4 = 'Post_Data';

   // Connect to all 4 Databases
        $connection1 = mysql_connect($host1, $username1, $password1) or die ('Cannot connect to the database because: ' . mysql_error());
        $connection2 = mysql_connect($host2, $username2, $password2, true) or die ('Cannot connect to the database because: ' . mysql_error());
        $connection3 = mysql_connect($host3, $username3, $password3, true) or die ('Cannot connect to the database because: ' . mysql_error());
        $connection4 = mysql_connect($host3, $username4, $password4, true) or die ('Cannot connect to the database because: ' . mysql_error());

   // Combine all 3 Databases into the Search Database #4
        mysql_select_db ($database1,$connection1);
        mysql_select_db ($database2,$connection2);
        mysql_select_db ($database3,$connection3);
        mysql_select_db ($database4,$connection4);

        mysql_query("USE $database4");
        mysql_query("CREATE TABLE temp AS
         SELECT * FROM $database1.$TableName1
         UNION
         SELECT * FROM $database2.$TableName2
         UNION
         SELECT * FROM $database3.$TableName3
         ");
        mysql_query("CREATE INDEX ix_post_date ON temp.Post_Date");
        mysql_query("RENAME TABLE Post_Data TO backup, temp TO Post_Data");

   // Close databases connections
        mysql_close($connection1);
        mysql_close($connection2);
        mysql_close($connection3);
        mysql_close($connection4);

   // Finished
        $date_time = date('m-d-Y H:i:s');
        echo '<h1>Finished - '.$date_time.'</h1>';

?>

You create 4 different connections to the database, but the mysql_query is going to run against one connection.

The query will need to know which database to connect to. You can see How do you connect to multiple MySQL databases on a single webpage? for a good example

You should be able to see that your create table query fails by displaying the error:

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM