Solved

create statements from a mysql dump file

Posted on 2014-01-01
5
333 Views
Last Modified: 2014-01-03
CREATE TABLE `a_images` (
  `a_images_id` int(11) NOT NULL auto_increment,
  `profile_id` varchar(20) default NULL,
  `image_name` varchar(100) default NULL,
  `image_url` varchar(200) default NULL,
  PRIMARY KEY  (`a_images_id`)
) ENGINE=MyISAM AUTO_INCREMENT=88 DEFAULT CHARSET=utf8;


insert into table
...

...


CREATE TABLE `a_images2` (
  `a_images_id` int(11) NOT NULL auto_increment,
  `profile_id` varchar(20) default NULL,
  `image_name` varchar(100) default NULL,
  `image_url` varchar(200) default NULL,
  PRIMARY KEY  (`a_images_id`)
) ENGINE=MyISAM AUTO_INCREMENT=88 DEFAULT CHARSET=utf8;


insert
...
...
...

CREATE TABLE `a_images3` (
  `a_images_id` int(11) NOT NULL auto_increment,
  `profile_id` varchar(20) default NULL,
  `image_name` varchar(100) default NULL,
  `image_url` varchar(200) default NULL,
  PRIMARY KEY  (`a_images_id`)
) ENGINE=MyISAM AUTO_INCREMENT=88 DEFAULT CHARSET=utf8;

Open in new window



this is a mysql dump file

using php please echo
only the create statements
beginning with
CREATE TABLE
and ending with
;
0
Comment
Question by:rgb192
  • 2
  • 2
5 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 39750567
<?php
$src = file_get_contents("yourdumpfile.sql");

preg_match_all("/CREATE TABLE [^;]+;/",$src,$matches);

print_r($matches[0]); // $matches[0] will have the statements.

?>
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39751236
I'm pretty sure @gr8gonzo has a good REGEX there, but I also wonder about the usefulness of the programmatic solution.  It took me less than 30 seconds to do it all by hand in my text editor.  See number 1205.

CREATE TABLE `a_images` (
  `a_images_id` int(11) NOT NULL auto_increment,
  `profile_id` varchar(20) default NULL,
  `image_name` varchar(100) default NULL,
  `image_url` varchar(200) default NULL,
  PRIMARY KEY  (`a_images_id`)
) ENGINE=MyISAM AUTO_INCREMENT=88 DEFAULT CHARSET=utf8;
CREATE TABLE `a_images2` (
  `a_images_id` int(11) NOT NULL auto_increment,
  `profile_id` varchar(20) default NULL,
  `image_name` varchar(100) default NULL,
  `image_url` varchar(200) default NULL,
  PRIMARY KEY  (`a_images_id`)
) ENGINE=MyISAM AUTO_INCREMENT=88 DEFAULT CHARSET=utf8;
CREATE TABLE `a_images3` (
  `a_images_id` int(11) NOT NULL auto_increment,
  `profile_id` varchar(20) default NULL,
  `image_name` varchar(100) default NULL,
  `image_url` varchar(200) default NULL,
  PRIMARY KEY  (`a_images_id`)
) ENGINE=MyISAM AUTO_INCREMENT=88 DEFAULT CHARSET=utf8;

Open in new window

0
 

Author Comment

by:rgb192
ID: 39754220
I understand Ray's link, but  gr8gonzo code appears to work in every mysql dump file so I can reuse many times



 gr8gonzo:
how to echo
without
[0]=>
[1]=>
[2]=>
[3]=>

so i can copy paste into my mysql query editor
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 39754232
Just do a foreach loop and dump the values:

foreach($matches[0] as $query)
{
  echo $query . "\n\n";
}
0
 

Author Closing Comment

by:rgb192
ID: 39755494
this code works to show all my table creates

thanks
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHPStorm debugging issues 1 45
echo paypal data on screen 5 50
How to init a part of Javascript based on PHP value for Datatables & Yadcf 8 48
PHP Installer 5 23
This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question