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 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.
shell_exec is disabled, backticks won't work.shell_exec is available or not and notify the user that this functionality is not available and/or provide a workaround. Take a look at the other answers here and I think there should be code in PHPMyAdmin and similar tools that do what you want. See as well: Loading .sql files from within PHP$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.
; will render this useless.; would pose a problem, maybe we can handle that with a preg_split()