0

I want to make a database restore function that will parse a dump made with phpMyAdmin and will execute all of the queries.

I already have a MySQL class that does the query, but it only knows how to do a single query. I am looking of a way to split the file content into single queries.

I tried to play with preg_split ... but didn't managed to get it to work.

$queries = preg_split('/[(.+);\s*\n/', $content, -1, PREG_SPLIT_NO_EMPTY);

I am not very good with regular expressions. Is there a way I can accomplish my goal?

3 Answers 3

5

don't try to parse sql with regular expressions. what if there is a ; somewhere inside a sql string?

i would try http://pear.php.net/package/SQL_Parser, or any of the other php mysql parsers out there. see also PHP MySQL SQL parser (INSERT and UPDATE) here on SO.

Sign up to request clarification or add additional context in comments.

Comments

0

I think splitting in comments above will make bad strings when you have stored procedures, functions or triggers in $content.

Comments

0

Eventually I managed to find the right regex:

$queries = preg_split('/[.+;][\s]*\n/', $content, -1, PREG_SPLIT_NO_EMPTY);

it splits the query after the ; sign and does not break even if the query spreads on multiple rows or the query contains ; since it looks for it at the end of a line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.