0

我正在尝试创建一个单独的搜索站点(使用 database4 的 site4 ),该站点每小时从 3 个不同的网站更新,每个网站都有自己的数据库。我想将database1、database2和database3中的数据合并到database4中。

我还想在合并过程中删除重复项,所以有人告诉我使用 MySQL UNION 函数。

在同一台服务器上,我有 4 个单独的网站,每个网站都有自己的 MySQL 数据库:

站点 1 --> 数据库 1,1 个表,16 个字段

站点 2 --> 数据库 2,1 个表,16 个字段

站点 3 --> 数据库 3,1 个表,16 个字段

site4 --> database4,1个表,16个字段(当前为空)

所有 4 个数据库都具有相同的结构,因为每个数据库只有 1 个包含 16 个字段的表。

在所有 4 个数据库中,表名相同(Post_Data),所有 16 个字段都相同。索引字段(字段 11)被命名为 Post_Date。

在一些论坛的帮助下,我能够编写以下 PHP 代码,但它不起作用并且我没有收到错误。

你能看出下面的代码有什么问题以及需要做些什么来修复它吗?

提前致谢。

<?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>';

?>
4

1 回答 1

0

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);
}
于 2013-06-19T18:53:08.253 回答