Php 将新行从本地表插入远程表

Php 将新行从本地表插入远程表,php,mysql,Php,Mysql,我想将新行从本地表插入远程表,我已经为它编写了一个php脚本,但它不起作用 远程和本地-数据库、表和字段相同 我是这样做的 //connection $remote_hostname='xxx.xxx.xxx.xxx:3306'; $hostname='localhost'; $username = 'username'; $password = 'password'; $remote_connection = mysql_connect($remote_hostname, $username,

我想将新行从本地表插入远程表,我已经为它编写了一个php脚本,但它不起作用

远程和本地-数据库、表和字段相同

我是这样做的

//connection
$remote_hostname='xxx.xxx.xxx.xxx:3306';
$hostname='localhost';
$username = 'username';
$password = 'password';
$remote_connection = mysql_connect($remote_hostname, $username, $password); 
$connection = mysql_connect($hostname, $username, $password); 

$tablename="pc_games";
$database = 'games';

// some row count here $remoterows

$local_query = "SELECT * FROM $tablename LIMIT 100 OFFSET $remoterows";
$local_result = mysql_query($local_query, $connection) or trigger_error(mysql_error()); 

while($list=mysql_fetch_array($local_result))
{
$remote_update=mysql_query("INSERT INTO $tablename SELECT * from $tablename");
$remote_update_result = mysql_query($remote_update, $remote_connection) or trigger_error(mysql_error());    
}
这不起作用,并显示错误
键“PRIMARY”的重复条目“1”
,但没有重复条目

如果我这样做,就会将新行插入到远程数据库中

while($list=mysql_fetch_array($local_result))
{
$id=$list['id'];    
$pflink=$list['pflink'];    
$image=$list['image'];  
$pagelink=$list['pagelink'];    
$title=$list['title'];
    // and so on... 

$remote_update=mysql_query("INSERT INTO $tablename SET id='$id', image='$image', pagelink='$pagelink', title='$title'......");
$remote_update_result = mysql_query($remote_update, $remote_connection) or trigger_error(mysql_error());    
}
我在数据库中有很多列,也有很多数据库,我想用第一种方法,因为我想在另一个数据库中重复使用这些代码,只需更改
$database
$tablename
在另一个数据库的必需文件中

while($list=mysql_fetch_array($local_result))
{
$id=$list['id'];    
$pflink=$list['pflink'];    
$image=$list['image'];  
$pagelink=$list['pagelink'];    
$title=$list['title'];
    // and so on... 

$remote_update=mysql_query("INSERT INTO $tablename SET id='$id', image='$image', pagelink='$pagelink', title='$title'......");
$remote_update_result = mysql_query($remote_update, $remote_connection) or trigger_error(mysql_error());    
}

请查看并建议任何可能的方法。

您不能在一个请求中跨越本地和远程查询:

$remote_update=mysql_query("INSERT INTO $tablename SELECT * from $tablename");
这应该是从本地选择中获取数据,然后将其插入远程数据库中


该查询在1个数据库上运行,并且仅在1个数据库上运行。您正在尝试从表中提取数据并将其插入到同一个表中。当然,这为键“PRIMARY”提供了一个重复的条目“1”

,您没有忘记更改$tablename变量吗?您可以通过“INSERT into tablename(AS)SELECT…”跳过双重选择并将数据插入另一个表中。啊,您希望在不同的数据库上使用相同的tablename。您能告诉我您在哪里创建$remote\u连接和$connection吗?请不要使用
mysql\u*
函数来编写新代码。它们不再得到维护,社区已经开始。看到了吗?相反,你应该学习并使用或。如果你不能决定哪一个,我会帮你的。如果您选择PDO,.ya,我将在编码完成后转换为mysqli。是的,此查询仅作用于本地数据库,您能告诉我任何方法吗。您回答了自己的问题。没有其他方法可以从本地获取数据并插入到远程。或设置数据库之间的复制。