从本地表向远程表插入新行 [英] Insert new rows from local table to remote table

查看:39
本文介绍了从本地表向远程表插入新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to insert new rows from local table to remote table, i have made a php script for it but it is not working.

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

Remote and local - database, table and fields are same.

我就是这样做的

//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());    
}

这不起作用并显示错误 Duplicate entry '1' for key 'PRIMARY',但没有重复的条目.

This is not working and showing error Duplicate entry '1' for key 'PRIMARY', but there is no duplicate entry.

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

If i do it this way it works, new rows get inserted into remote database.

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在另一个数据库的 require 文件中.

I have a many colums in database and also many database, i want to do it the first way, as i want to re use these codes for another database with just changing $database and $tablename in require file for another database.

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

Please see and suggest any possible way to do it.

推荐答案

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

You cannot span a local and remote query in one request:

$remote_update=mysql_query("INSERT INTO $tablename SELECT * from $tablename");

这应该从本地选择获取数据并将其插入远程数据库?

This is supposed to get data from the local select and insert it into the remote database?

查询对 1 个数据库进行操作,并且仅对 1 个数据库进行操作.您正在尝试从表中获取数据并将其插入同一个表中.当然,这为键 'PRIMARY' 提供了一个 Duplicate entry '1'

The query operates on 1 database, and 1 database only. You are trying to fetch data from a table and insert it on the same table. And of course, this gives a Duplicate entry '1' for key 'PRIMARY'

这篇关于从本地表向远程表插入新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆