howto run sql script with php?
| From: | Stephan Ebelt | Date: | Sun, 13 Jun 2010 09:05:42 +0000 |
| Subject: | howto run sql script with php? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-306081@lists.php.net to get a copy of this message | ||
Hello,
I am trying to run a .sql script produced with mysql-dump from a php script but
failed to find a rudimentary robust solution yet.
mysql_query() can only run one query at a time. Thus its necessary to somehow
parse the input file and separate the queries... which is difficult since the
syntax can be quite complex.
So far I managed to run most CREATE TABLE and INSERT queries just by
dropping newlines (\n) and explode()ing the input by ';'. Now I am stuck with a
CREATE TRIGGER statement because mysql-dump does this:
===
DELIMITER ;;
CREATE TRIGGER ... ON ...
FOR EACH ROW BEGIN ...
...
END;
;;
DELIMITER ;
===
exploding that string by ';' results in 4 (or more) queries which are invalid
syntax then. And I have no glue on what else mysql-dump may come up with on
other occasions.
does anyone know how to do that properly?
I dont favor importing the dumps via shell command like 'cat some.dump | mysql'
either.
stephan