Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

PHP regex to extract SQL queries

Last post 07-02-2008, 7:12 AM by merianos. 2 replies.
Sort Posts: Previous Next
  •  07-01-2008, 3:32 AM 43635

    PHP regex to extract SQL queries

    Hello all . . .

     I need a regex to extract the MySQL queries from an sql file

     The SQL file content it looks like this :

     #
    #  dbtables.sql
    #
    #  Simplifies the task of creating all the database tables
    #  used by the login system.
    #
    #  Can be run from command prompt by typing:
    #
    #  mysql -u yourusername -D yourdatabasename < dbtables.sql
    #
    #  That's with dbtables.sql in the mysql bin directory, but
    #  you can just include the path to dbtables.sql and that's
    #  fine too.
    #
    #  Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
    #  Last Updated: August 13, 2004
    #

    #
    #  Table structure for users table
    #
    DROP TABLE IF EXISTS users;

    CREATE TABLE users (
    username varchar(30) primary key,
    password varchar(32),
    userid varchar(32),
    userlevel tinyint(1) unsigned not null,
    email varchar(50),
    timestamp int(11) unsigned not null
    );


    #
    #  Table structure for active users table
    #
    DROP TABLE IF EXISTS active_users;

    CREATE TABLE active_users (
    username varchar(30) primary key,
    timestamp int(11) unsigned not null
    );


    #
    #  Table structure for active guests table
    #
    DROP TABLE IF EXISTS active_guests;

    CREATE TABLE active_guests (
    ip varchar(15) primary key,
    timestamp int(11) unsigned not null
    );


    #
    #  Table structure for banned users table
    #
    DROP TABLE IF EXISTS banned_users;

    CREATE TABLE banned_users (
    username varchar(30) primary key,
    timestamp int(11) unsigned not null
    );

     

    Where # means the following text is comments for the programmes and etc.

    and the queries end with the english question mark ( ; )

     

    So the question is how to extract all the queries with or without their own comments  ? ? ?

    One aproach that I have try is this :

     /(^#|^[a-zA-Z])(.+)\;/m

    And i have analize it like that get anythink that starts with # or a-zA-Z characters followed by any character and ends with ;

    but this aproach return all the file content line by line

    My PHP code is this :

    function extractQueries2()
        {
            $fileSize = 0;
            $query = '';
           
            if(strlen($this->fileName) < 1)
            {
                return '';
            }
            else
            {
                $file = @fopen("$this->fileName", "r");//open file for reading
                $rf = fread($file, 32000); // read 32K
                fclose( $file );
                
       $rg = "/(^#|^[a-zA-Z])(.+)\;/m";
          
                $rs = $this->do_reg($rf, $rg);
       for($i = 0; $i < sizeof($rs) - 1; $i++)
       {
        echo $rs[$i] . "<br /><br />";
       }
            }
        }
        
     private function do_reg($text, $regex)
     {
      preg_match_all($regex, $text, $result, PREG_PATTERN_ORDER);
      return $result = $result[0];
     } 

    Many Many Thanks . . . ! ! !


    Datacenter Hellas
  •  07-01-2008, 10:07 AM 43660 in reply to 43635

    Re: PHP regex to extract SQL queries

    preg_replace('{(?m)^\s*#.*[\r\n]}',"",$contents);

    Where $contents is the content of that dump

    $contents=file_get_contents($filename);


    http://portal-vreme.ro
  •  07-02-2008, 7:12 AM 43685 in reply to 43660

    Re: PHP regex to extract SQL queries

    Thanks a lot . . .

    This was realy very helpfull ! ! !

    After your instructions i used the split function to split the queries based on question mark ; and it's working perfectly.

     Many many Thanks !  ! !


    Datacenter Hellas
View as RSS news feed in XML