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

  • SEARCH
  • Home
  • 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 8036693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:42:09+00:00 2026-06-05T02:42:09+00:00

What is the best way to copy database from one to another with specific

  • 0

What is the best way to copy database from one to another with specific date?
Here is the flow, the user do some action on my program (PHP Language) and then get some file. Then that file he need to upload on the other program so the database copied.

What will be copied will is only the data without structure, since structure is always same.

Example here is the structure the data

Source table called old

id | date       | value_1   | value_2   | value 3
1  | 2012-06-02 | some_data | some_data | some_data
2  | 2012-06-03 | some_data | some_data | some_data
3  | 2012-06-03 | some_data | some_data | some_data

and then the user want to download the data only for the 2012-06-03 to moved it into the new table called new. And the result in the new will be:

id | date       | value_1   | value_2   | value 3
2  | 2012-06-03 | some_data | some_data | some_data
3  | 2012-06-03 | some_data | some_data | some_data

Notes:

[*]The program will be need smart enough to understand, if the `id` already in the `new` database then do `update` if not do `insert`.
[*]The table is located on different database. e.g: `db_source.old` copied to `db_target.new`

What is the best logic to do that? or maybe is there already some classes to handle this kind of task?


Please understand this is not college task, but real project implementation that i want to add into my program. And I’m asking here is not for get the code question. Please give me some clue then i will be happy to research it


Already made some script that output the data.

Some snippet that do the gathering data script

/**
 * Get all database data and output it in array format
 * @return array
 */
function output(){
    $return = array();
    foreach($this->getAllTableName() as $table_name){
        $this->load->model($table_name.'_model');
        $class_name = $table_name.'_model';
        $model = new $class_name;

        $return[$table_name] = array();
        $this->db->where('created >=', $this->date_start);
        $this->db->where('created <=', $this->date_end);            
        foreach((array)$model->get() as $object){               
            $return[$table_name][$object->id] = $this->wrap_value($object);
        }           
    }

    return $return;
}

The output:

enter image description here

but still confused with the SQL file will be written to understand which data need to update and insert

  • 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-06-05T02:42:10+00:00Added an answer on June 5, 2026 at 2:42 am

    Do a stored procedure that accepts the id numbers (maybe in an array or something) and moves the data. Are the tables names hard-coded? Are the databases hard-coded? You can always write a stored procedure for each table and do the logic to choose which procedure in php. If the databases are not hard-coded you could always take the in as a parameter.

    Not sure the exact coding but how about something like this:

    Have the @ids passed as “id1, id2, id3…” or an array. Use them with the in clause. Also use insert-on-duplicate

    INSERT INTO new.table (id,date,value_1, value_2, value_3) 
      select id, date, value_1, value_2, value_3 
        from old.table 
        where id in (@ids)
      ON DUPLICATE KEY UPDATE c=3;
    

    You just have to replace c=3 with your update. Here is an example of an update with join from here

    UPDATE FROM tblTransaction AS t 
    LEFT JOIN tblEmployee as e 
    ON e.emp_id = t.emp_id 
    SET t.emp_block = e.emp_block
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to create a copy of all data from database
Anoter question: What is the best way to copy a whole table from one
I need to copy a table from one database to another. This will be
Possible Duplicate: Best way to copy the entire contents of a directory in C#
In PHP, what's the best way to copy an array and keep the keys
What is the best way to view the HTTP response headers from a request
Dumb question - what's the best way to copy instances in an environment where
I have a master page that needs to display some data from my database.
Using SQL Server Management Studio, I have backed up a database from one server
I need to create a static copy of values from one table and store

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.