views:

31

answers:

2

How can i fetch all data from any table of a test DB and DUMP it on the production db in same tablename(structure)? in Mysql using PHP code?

Please help me..

EDIT

I need PHP to do this for me using MySQL Queries

A: 

If you mean by dumping creating SQL code to recreate the table, I'd suggesting using existing tools to manage MySQL such as phpMyAdmin or Adminer.

Michal Čihař
I need PHP to do this for me using MySQL Queries
OM The Eternity
+1  A: 

Sorry, a little disorganized but these are the functions I use to build the SQL code for table reconstruction and repopulating. This is all within a 'table' object but I'm sure you can piece together what's going on.

The SQL "SHOW CREATE TABLE yourtablename" gives you everything needed to create the table again. All the rest is just looping through all the entries and making INSERT statements.

public function backup() {
    $output = "/* - - - Table recovery for $this->tablename - - - */\n\n\n";
    $output .= "DROP TABLE IF EXISTS `$this->tablename`;\n\n";
    $output .= $this->showcreate().';';
    $output .= $this->showrepopulate();
    return $output."\n\n\n";
}        


final protected function showcreate() {
    $result = $this->query("SHOW CREATE TABLE $this->tablename");
    return $result[0]['Create Table'];
}

final protected function showrepopulate() {
    $result = $this->query("SELECT * FROM $this->tablename");
    $output = '';
    $count = count($result);
    if ($count) {
        $output = "\n\nINSERT INTO `$this->tablename` VALUES\n";
        for ($i=0; $i<$count; $i++) {
            $output .= "(";
            $numfields = count($result[$i]);
            for ($j=0; $j<$numfields; $j++) {
                if ($j>0) $output .= ',';
                $output .= $this->Database->escape(array_shift($result[$i]));
            }
            $output .= ")";
            $output .= ($i != $count-1) ? ",\n" : ';';
        }
    }
    return $output;
}
Lotus Notes
If you are using InnoDB and the table in question is used in foreign keys, you might want to disable checking for foreign keys before importing (otherwise it will fail). BUT : make sure you check your foreign keys before importing because MySQL will happily live with inconsistent data even when you turn foreign_key_checks back on... On the bright side : you can use the stored procedure on http://forge.mysql.com/tools/tool.php?id=11 to check your data after import.
wimvds