Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How do I Export mysql database tables to php code so that it allows me to create and populate same tables in other database?

I have a local database, I exported to sql syntax, then I get something like:

CREATE TABLE `boletinSuscritos` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(120) NOT NULL,
  `email` varchar(120) NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

INSERT INTO `boletinSuscritos` VALUES(1, 'walter', 'waltermazzola@hotmail.com', '2010-03-24 12:53:12');
INSERT INTO `boletinSuscritos` VALUES(2, 'Paco', 'paco@arroba.com', '2010-03-24 12:56:56');

but I need it to be: (Is there any way to export the tables in this way)

$sql = "CREATE TABLE  boletinSuscritos  (
   id  int(11) NOT NULL AUTO_INCREMENT,
   name  varchar(120) NOT NULL,
   email  varchar(120) NOT NULL,
   date  timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY ( id )
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 )";

mysql_query($sql,$conexion);

 mysql_query("INSERT INTO boletinSuscritos  VALUES(1, 'walter', 'pepe@hotmail.com', '2010-03-24 12:53:12')");
 mysql_query("INSERT INTO boletinSuscritos  VALUES(2, 'Paco', 'paco@arroba.com', '2010-03-24 12:56:56')");
share|improve this question
    
sorry about the lack of breaklines, but when creating this question it looks with breaklines... –  chefnelone Jun 29 '11 at 16:28
    
There's a button to format the codes. i.imgur.com/29ueO.png –  Sathya Jun 29 '11 at 16:32

1 Answer 1

up vote 0 down vote accepted

You can export your databases to a file using this command:

  • mysqldump -u username -p password dbname > dump.sql

If you would prefer to only dump the structure of the database:

  • mysqldump -u username -p password –no-data dbname > dump.sql

You can then import your database using a similar command:

  • mysql -u username -p password dbname < dump.sql

Since you want to do this from within php, you can do something along these lines:

$result = 0;
$command = "mysqldump -u username -p password dbname > dump.sql";
exec($command,$output=array(),$result);
if($result) echo "yay, this has worked.\n";

You may be able to find more examples by figuring out how PHPMyAdmin does it, or checking out MySQL's Load Data function.

share|improve this answer
    
I understand that I need to run these commands in the Terminal (mac) right? Problem for me is that I use MAMP and the Terminal doesn't realize that mysql is installed then the mysql commands doesn't work in the terminal. I'll take a look at the link you posted. Thanks –  chefnelone Jun 30 '11 at 7:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.