【问题标题】:Compare two tables from different servers using PHP使用 PHP 比较来自不同服务器的两个表
【发布时间】:2011-01-10 22:25:49
【问题描述】:

我需要从两个独立服务器上的两个独立数据库中检索数据(用户名和电子邮件地址),并检查用户名是否仅存在于一侧而不存在于另一侧,并将结果显示在表格中。根据我的研究,不可能在 mySQL 中进行跨服务器查询,所以我试图在 PHP 中完成这一切,但我遇到了没有结果的问题,而且我的代码变得不可读。主要问题是我需要尽可能输出与用户名关联的电子邮件地址,但只有第一个数据库有用户名和电子邮件地址列表。我确信我的算法完全没有效率,我很想看到一些简单的解决方案。

$handle1 = mysql_connect($db_host1,$db_username1,$db_password1);
mysql_select_db($db_name, $handle1);

$query1 = "SELECT username, email, FROM tblservices";
$result1 = mysql_query($query1);

while($row = mysql_fetch_array($result1){
$arr1[]=array(username=>$row['username'],email=>$row['email']);}

$handle2 = mysql_connect($db_host2,$db_username2,$db_password2);
mysql_select_db($db_name, $handle2);

$query2 = "SELECT username from tblradius";
$result2 = mysql_query($query2, $handle2);

while($row = mysql_fetch_array($result2)){
    $arr2[]=$row['username'];
}


echo "<p>List of users in DB1 and not DB2</p>";

$x=0;
foreach($arr1 as $row1) {
    $user1=$row1['username'];
    $email=$row1['email'];
        foreach($arr2 as $user2) {
            if ($user1==$user2) {
                $x=1;
            }
        }
        if ($x==0) {
            echo "<p>$user1 - $email is not in DB2</p>";
        }
    $x=0;
    }
}

echo "<p>List of users in DB2 and not DB1</p>";

$x=0;
foreach($arr2 as $user1) {
    foreach($arr1 as $row2) {
        $user2=$row2['username'];
        if ($user1==$user2) {
            $x=1;
        }
    }
    if ($x==0) {
        foreach($arr2 as $row3) {
            $user3 = $row3['username'];
            $email = $row3['email'];
            if ($user1==$user3) {
                echo "<p>$user3 $email - is in DB2 and not DB1</p>";
            } 
        }
    }
    $x=0;
}

}

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    这样的事情应该可以解决问题

    //connect and query DB1
    
    while($row = mysql_fetch_assoc($result1)) {
        $users[$row['username']] = $row['email'];
    }
    
    //Connect and query DB2
    
    while($row2 = mysql_fetch_assoc($result2)) {
        if(isset($users[$row2['username']])) {
            unset($users[$row2['username']]);
        } else {
            $db2users[$row2['username']] = "No mail given for user";
        }
    }
    
    foreach($users as $username => $email) {
        echo $username . " - " . $email . " does not exist in DB2";
    }
    foreach($db2users as $db2username => $db2email) {
        echo $db2username . " - " . $db2email . " does not exist in DB1";
    }
    

    它的作用是将第一个查询中的所有用户添加到一个数组中,然后如果它们存在于第二个数组中,则根据数组键将其删除,这也使性能也很好。

    最后的数组将包含所有存在于 DB1 但不存在于 DB2 中的用户的姓名和邮件。

    省略与数据库的连接和查询,因为您已经有了这些。

    【讨论】:

      【解决方案2】:

      通过使用电子邮件地址作为第一个数组中的键来简化数组:

      <?php
      $array1 = array('user1@example.com' => 'user1',
                      'user2@example.com' => 'user2',
                      'user3@example.com' => 'user3',
                      'user4@example.com' => 'user4');
      $array2 = array('user2', 'user4');
      $dif = array_diff($array1, $array2);
      print_r($dif);  // outputs Array ( [user1@example.com] => user1 [user3@example.com] => user3 )
      ?>
      

      $dif 在 DB1 中为您提供一组用户,但在 DB2 中没有。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-22
        • 2016-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-22
        • 1970-01-01
        • 2015-10-02
        相关资源
        最近更新 更多