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?

Jason
  • 15,017
  • 23
  • 85
  • 116
Gabriel Solomon
  • 29,065
  • 15
  • 57
  • 79

3 Answers3

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.

Community
  • 1
  • 1
ax.
  • 58,560
  • 8
  • 81
  • 72
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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gabriel Solomon
  • 29,065
  • 15
  • 57
  • 79
0

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

Guy Fawkes
  • 2,313
  • 2
  • 22
  • 39