0

我现在有一个实时服务器和一个测试服务器,我需要从实时复制一个表以使用 cron 进行测试,因为它需要每小时运行一次。我已经创建了一个php脚本并使用 localhost 对其进行了测试,它工作正常,但是当我在实时服务器上测试时,它不起作用。

我怀疑是因为在本地主机上时,虽然它在不同的数据库之间但仍然使用相同的服务器连接。我正在使用phpmyadmin.

编辑:这里是代码

<?php
/* fill in your source database name */
$database = "xxx";
$dbhost1 = "xxx";
$dbusr1 = "xxx";
$dbpas1 = "xxx";
/* fill in your target database name */
$database2 = "yyy";
$dbhost2 = "yyy";
$dbusr2 = "yyy";
$dbpas2 = "yyy";

if ($connect = mysqli_connect($dbhost1, $dbusr1, $dbpas1)) 
{
    mysql_select_db($database, $connect);
    echo "connected to xxx<br/>";
}
else { die("Source database fail to connect: Please try again" . mysql_error());}

if ($connect2 = mysqli_connect($dbhost2, $dbusr2, $dbpas2)) 
{
    mysql_select_db($database2, $connect2);
    echo "connected to yyy<br/>";
}
else { die("Target database fail to connect 1: " . mysql_error());}
set_time_limit(0);

$tables = array("coupon");
$tables_no = count($tables); 

for ($i=0; $i < $tables_no ; $i++){
    $tab = $tables[$i];
    $query_table1 = "SELECT * FROM $database.$tab";
    $query_table2 = "SELECT * FROM $database2.$tab";

    $source= mysql_query($query_table1);
    $destination= mysql_query($query_table2);

    if ($source <= $destination){
    echo 'aaa';
        while($row_table1 = mysql_fetch_array($source)){
            // var_dump ($row_table1);
            // if statement
        }
    }
    else {echo 'dalam else';}
} 
?>
4

1 回答 1

0

您的第二次连接正在杀死您的第一次。如果使用mysql_connect,请查看手册中要传入的变量以避免这种情况...

if ($connect = mysql_connect($dbhost1, $dbusr1, $dbpas1)) 
{
    mysql_select_db($database, $connect);
    echo "connected to xxx<br/>";
}
else { die("Source database fail to connect: Please try again" . mysql_error());}

if ($connect2 = mysql_connect($dbhost2, $dbusr2, $dbpas2, true)) 
{
    mysql_select_db($database2, $connect2);
    echo "connected to yyy<br/>";
}

并从每个数据库中检索结果,使用它们各自的资源......

$result1 = mysql_query($connect1, $sql_query1);

$result2 = mysql_query($connect2, $sql_query2);
于 2013-08-27T02:17:39.290 回答