Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

create statements from a mysql dump file

Posted on 2014-01-01
5
Medium Priority
?
345 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 35

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 111

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 35

Accepted Solution

by:
gr8gonzo earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

885 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