简体   繁体   中英

Excute a MySQL script from within a PHP script?

Can you execute a MySQL script (foo.sql) from 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.

how?

bar.php:

<?php `mysql < foo.sql`;

see Execution Operators Docs and Using mysql in Batch Mode Docs .

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 .

mysqli :: multi_query是另一个选择。

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM