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 153343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:47:04+00:00 2026-05-11T09:47:04+00:00

I need to use PHP to copy data from one MySQL database to another.

  • 0

I need to use PHP to copy data from one MySQL database to another.

I can build and array of all the values to go into the other database but first I want to make sure the database has the correct fields before inserting.

For example say I am going to be copying data from tableA to tableB.

I can set up tableB to look just like tableA but in the future I may add columns to tableA and forget to add them to tableB, then my PHP script will try to insert data into a column that doesn’t exist in tableB and it will fail.

So what I want to do is compare tableA to tableB and any columns that tableA has that tableB doesn’t have add them to tableB.

Can anyone tell me how to do this?

  • 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. 2026-05-11T09:47:04+00:00Added an answer on May 11, 2026 at 9:47 am

    Thanks everyone, based on all your help I was able to write a PHP class that copies any columns from table A to table B if they are not already there:

    class MatchTable  {     var $_table_one_name;     var $_table_two_name;      var $_table_one_db_user;     var $_table_one_db_pass;     var $_table_one_db_host;     var $_table_one_db_name;      var $_table_two_db_user;     var $_table_two_db_pass;     var $_table_two_db_host;     var $_table_two_db_name;      var $_table_one_columns = array();     var $_table_two_columns = array();     var $_table_one_types = array();     var $_table_two_types = array();      var $_table_one_link;     var $_table_two_link;      var $_isTest;       function MatchTable($isLive = true)     {         $this->_isTest = !$isLive;     }      function matchTables($table1, $table2)     {         $this->_table_one_name = $table1;         $this->_table_two_name = $table2;          if(isset($this->_table_one_db_pass))         {             $this->db_connect('ONE');         }         list($this->_table_one_columns,$this->_table_one_types) = $this->getColumns($this->_table_one_name);          if(isset($this->_table_two_db_pass))         {             $this->db_connect('TWO');         }         list($this->_table_two_columns,$this->_table_two_types) = $this->getColumns($this->_table_two_name);          $this->addAdditionalColumns($this->getAdditionalColumns());     }      function setTableOneConnection($host, $user, $pass, $name)     {         $this->_table_one_db_host = $host;         $this->_table_one_db_user = $user;         $this->_table_one_db_pass = $pass;         $this->_table_one_db_name = $name;     }      function setTableTwoConnection($host, $user, $pass, $name)     {         $this->_table_two_db_host = $host;         $this->_table_two_db_user = $user;         $this->_table_two_db_pass = $pass;         $this->_table_two_db_name = $name;     }      function db_connect($table)     {         switch(strtoupper($table))         {             case 'ONE':                 $host = $this->_table_one_db_host;                 $user = $this->_table_one_db_user;                 $pass = $this->_table_one_db_pass;                 $name = $this->_table_one_db_name;                 $link = $this->_table_one_link = mysql_connect($host, $user, $pass, true);                 mysql_select_db($name) or die(mysql_error());             break;             case 'TWO';                 $host = $this->_table_two_db_host;                 $user = $this->_table_two_db_user;                 $pass = $this->_table_two_db_pass;                 $name = $this->_table_two_db_name;                 $link = $this->_table_two_link = mysql_connect($host, $user, $pass, true);                 mysql_select_db($name) or die(mysql_error());             break;             default:                 die('Improper parameter in MatchTable->db_connect() expecting 'one' or 'two'.');             break;         }         if (!$link) {             die('Could not connect: ' . mysql_error());         }     }      function getColumns($table_name)     {         $columns = array();         $types = array();         $qry = 'SHOW COLUMNS FROM '.$table_name;         $result = mysql_query($qry) or die(mysql_error());         while($row = mysql_fetch_assoc($result))         {             $field = $row['Field'];             $type = $row['Type'];             /*             $column = array('Field' => $field, 'Type' => $type);             array_push($columns, $column);             */             $types[$field] = $type;             array_push($columns, $field);         }         $arr = array($columns, $types);         return $arr;     }      function getAdditionalColumns()     {         $additional = array_diff($this->_table_one_columns,$this->_table_two_columns);         return $additional;     }      function addAdditionalColumns($additional)     {         $qry = '';         foreach($additional as $field)         {             $qry = 'ALTER TABLE '.$this->_table_two_name.' ADD '.$field.' '.$this->_table_one_types[$field].'; ';              if($this->_isTest)             {                 echo $qry.'<br><br>';             }             else             {                 mysql_query($qry) or die(mysql_error());             }         }     }      /**      * End of Class      */ } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Whats happening here is the code between the <% and… May 11, 2026 at 11:51 pm
  • Editorial Team
    Editorial Team added an answer No conversion is needed for that as far as I… May 11, 2026 at 11:51 pm
  • Editorial Team
    Editorial Team added an answer From what you are trying to do it sounds like… May 11, 2026 at 11:51 pm

Related Questions

[edit] We're collecting credit application data from users on a web form. I have
Before you answer this I have never developed anything popular enough to attain high
I'm in the process of trying to migrate my ASPNET site to Django. All
I need to frequently duplicate one database on my MySQL server to a mirror

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.