0

When I try to copy database table from one db to another via phpmyadmin copy table to(database.table) operation function everything works fine, but when I do with the below code it works but some value in database table is turned to be null. So can I know how to make exact copy of the database table from one database to another is there is any other way to do so and what is error in the below code which make some tables null.

Note: Only the database table not whole database.

Here is the code

<?php

  if (!$con = new mysqli('localhost', 'username', 'password', 'db_movies2')) 
       {
         die('An error occurred while connecting to the MySQL server!<br><br>'.
             $con->connect_error);
       }
  $sql = array('DROP TABLE IF EXISTS ba_content;',
               'CREATE TABLE ba_content SELECT * FROM db_movies1.ba_content');

  if (sizeof($sql) > 0) 
       {
         foreach ($sql as $query)
           {
             if (!$con->query($query)) 
                {
                    die('A MySQL error has occurred!<br><br>' . $con->error);
                }
           }
      }

  $con->close();
?>

Here is example of database in which changes that occur automatically when database table is copied via the above PHP code it occurs in most of the tables.

-  `content_name` varchar(255) DEFAULT NULL,
-  `type` varchar(25) DEFAULT NULL,
+  `content_name` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
+  `type` varchar(25) CHARACTER SET utf8 DEFAULT NULL,
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Balaji JB
  • 29
  • 1
  • 2
  • Check this. May be this helps. http://stackoverflow.com/questions/3280006/duplicating-a-mysql-table-indexes-and-data – Milan Malani Sep 14 '16 at 05:47
  • i don't think you can do multi `SOL` statements in one query , maybe that's the problem .Not sue . Does it gave you any errors ? – Laith Sep 14 '16 at 06:09
  • what do you mean by "which make some tables null"? – Jakumi Sep 14 '16 at 08:30
  • I believe the `CREATE TABLE ... SELECT` queries don't actually copy the structure but will instead create a new structure that matches the attributes generated by the `SELECT` query, which could very well drop or add properties to your "new" fields compared to the original (the type should remain the same though). – Jakumi Sep 14 '16 at 08:31
  • Jakumi, I hope something make sense with your answer. Can you provide some documentation to do so, as Milan Malani comment in which as select. – Balaji JB Sep 14 '16 at 09:34

0 Answers0