Excute a MySQL script from within a PHP script?
Can you execute a MySQL script (foo.sql) from开发者_如何学Python within a PHP script (bar.php)? If so, how?
And, is this a recommended or not recommended practice, why or why not?
Thanks in advance.
mysqli::multi_query is another option.
how?
bar.php:
<?php `mysql < foo.sql`;
see Execution OperatorsDocs and Using mysql in Batch ModeDocs.
is this a recommended [...] practice, why [...] ?
It's always recommended to choose the right tool for the job. the mysql commandline interface is pretty powerful, fast and well-tested. It does what you're looking for.
Related: Loading .sql files from within PHP and Best practice: Import mySQL file in PHP; split queries.
$contents = get_file_contents( 'foo.sql' );
$queries = explode( ';', $contents );
foreach( $queries as $query ) {
mysql_query( $query );
}
I don't think anything's wrong with doing that.
精彩评论