0

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.

3 Answers 3

2

mysqli::multi_query is another option.

Sign up to request clarification or add additional context in comments.

1 Comment

Yea it will also be Turbo Pascal incompatible. Are you one of the little people who would still care?
2

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.

5 Comments

Do ticks equate to running a command line executable? Note, I had no idea.
@Jared Farrish: Yes they do. Like in shell scripts, compare: Execution Operators.
Thanks for your response hakre. Using back ticks in PHP (to execute its contents as a shell command) assumes that php.ini allows it, i.e. shell_exec is enabled. In this case, I can't assume that. Also, don't you have to be logged in to the mysql client to invoke it like that from within a php script or even in the command line?
@Rylie: That command has support for username and password, I've linked the docs, it's all in there. If shell_exec is disabled, backticks won't work.
@Rylie: If backticks are not available, you can not do it this way. If you can not say, you need to check if 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
0
$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.

12 Comments

Well, insert statements or comments containing ; will render this useless.
@hakre Yes you're right, also strings containing ; would pose a problem, maybe we can handle that with a preg_split()
Do not even try to improve your script. You will fail unless you write a full blown MYSQL language SQL parser, but there is no need, the CLI client has it already. Alternatively, the PHPMyAdmin code might have such.
@harke you might be right, actually I don't have time to write the regex to try this out, but if one doesn't have access to mysql executable (php safe mode for example) this might be the only option.
Not the only option, there is always the other option to just get access to the mysql executable. Don't solve with code what you can solve with configuration.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.