Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6337845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:19:45+00:00 2026-05-24T19:19:45+00:00

I have two databases($db1, $db2) with exact table structure(table1). $db2.table1 has new rows which

  • 0

I have two databases($db1, $db2) with exact table structure(table1). $db2.table1 has new rows which i want to insert into $db1.table1 (i.e. if $db2 is new and $db1 is old, I want to update $db1 with new entries from $db2).
I came up with following php code, and it should work fine, but I am worried about special characters in column ids as well as values to be inserted in it.
Here’s the code:

require('update_functions.php'); //contains helper functions

function arraywalk_mysql_real_escape_string(&$value, &$key, $sql_connection) {
    $value = mysql_real_escape_string($value, $sql_connection);
    $key = mysql_real_escape_string($key, $sql_connection);
}

$sql_connection_old = connectdb('old');
$sql_connection_new = connectdb('new');

$table = 'member'; $pkey = 'id'; //table name and primary key
$last_row_member = mysql_fetch_assoc(last_row($sql_connection_old, $table, $pkey));
//fetches last row from old db

$new_row = new_row($sql_connection_new, $pkey, $last_row_member[$pkey], $table, 'ASC LIMIT 1');
//the new_row function executes following query (after sanitizing input)
//'SELECT * FROM '.$table.' WHERE '.$pkey.'>'.$pkey_value.' ORDER BY '.$pkey.' '.$extra

while($result = mysql_fetch_assoc($new_row)) {
    array_walk($result, 'arraywalk_mysql_real_escape_string', $sql_connection_old);
    $update_member_query = 'INSERT INTO ' . $table . '( '
                     . implode(", ", array_keys($insert_vars))
                     . ' ) VALUES ( \''
                     . implode("', '", $insert_vars)
                     . '\' )';

}

I don’t know if there will be any special characters in column names. Should I enclose them in backticks?
If yes then should I parse them using mysql_real_escape_srting()?
What about VALUES ? Should I enclose them in quotes 'value'? What if the value to be inserted is a number? what if its Date/Time?
Is there a way where I can bypass fetching data from old database into PHP variables and inserting it back to database (so above questions become irrelevant)?

Note : Even though there are two connections, I have the same SQL server serving the two $db

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:19:46+00:00Added an answer on May 24, 2026 at 7:19 pm

    You can do it in SQL:

    INSERT INTO db1.table1
        SELECT * FROM db2.table1 
        WHERE db2.table1.id > (SELECT MAX(id) FROM db1.table1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two databases: DB1 and DB2 How to do something like: update myServer.DB1.dbo.hotels.Name
I have two databases called DB1 and DB2 on the same database server. I
Lets say we have two databases, one named db1, the other one named db2.
I have two databases at the same server 192.168.1.100 DB1 and DB2 When I'm
I have two databases for logging stuff, which I want to check for synchronization.
Basically I have a two databases on SQL Server 2005. I want to take
I have a web application that uses two databases. DB1 Users perform their CRUD
I have two mysql db on one machine. db1 and db2. If db1 is
I have two tables from two different databases. For example: Table: Articles1 - Database:
I have two databases, A, and B. On database A, I have a table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.