简体   繁体   中英

Updating remote database table from local database table

I want to update remote database from local database, I have written php script to do that but it is showing me fatal error.

Remote database and local database has same name, same table, same fields.

I have tried this way but its not working.

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

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

    while($list=mysql_fetch_array($local_result))
    {
    $remote_update=mysql_query("INSERT INTO $tablename SETLECT * from $tablename");
    $remote_update_result = mysql_query($remote_update, $remote_connection) or trigger_error("SQL", E_USER_ERROR);
    }

Please see and suggest any possible approach to do this.

"INSERT INTO $tablename SETLECT * from $tablename"

This is an invalid SQL statement. SETLECT have to be SELECT

Your remote query has a syntax error: "INSERT INTO $tablename SETLECT * from $tablename" . You mean SELECT instead of SETLECT .

Replacement code:

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

$local_query = "SELECT * FROM $tablename LIMIT 100 OFFSET $remoterows";
$local_result = mysql_query($local_query, $connection) or die(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 die (mysql_error());
}

What you did wrong, was you mistyped this line:

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

Notice that you used SETLECT , instead of what you meant to use (perhaps): SELECT .

I also marvel at how long it takes to detect a typing error.

I changed all your error handlers to the ones I mentioned in the comments. Use them in the future. They're better.

UPDATE: What did you do with your code? I just realised something drastic:

$remote_update=mysql_query("INSERT INTO $tablename SELECT * from $tablename");
$remote_update_result = mysql_query($remote_update, $remote_connection) or die (mysql_error());

First of all, you're running mysql_query on a result. Second, you aren't inserting anything INSERT INTO $tablename . Is it my fault, or are you doing something dreadfully wrong?

  1. Why are you looping through the results(with while($list=mysql_fetch_array($local_result)) ) when you absolutely don't need it?

  2. Why have you set the error function everywhere as: trigger_error("SQL", E_USER_ERROR); .

  3. Why are you still using mysql_* method for databases?

  4. If you just need to insert data from another table, just put:


mysql_query("INSERT INTO $tablename SELECT * FROM $tablename LIMIT 100 OFFSET $remoterows") or die(mysql_error());

and be done with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM