• Skip to primary navigation
  • Skip to main content

Muhammad Ikhwan

A collection of thoughts on a meaningful existence

  • About
  • Blog
  • Contact

Execute SQL File Through PHP

posted on August 19, 2010

Sometimes, you meet in a number of cases that need you to execute SQL file through PHP, in this case MySQL schema. Here is the script I been using until today.

$sqlFileLocation = '/path/to/sql/file.sql';
$sqlFileLocation =  getcwd().$sqlFileLocation;
$sqlSyntax = "mysql -u$mysql_user -p$mysql_pass $database_name < $sqlFileLocation";
$pid = @popen($sqlSyntax, "r");
@pclose($pid);

Make sure safe_mode is enabled, for better security. You could use this script such as for manual backup via web interface. Its even better if you authenticate/filter/limit which user could execute shell command from PHP because the malicious user could inject malicious script to your system. Be aware of that.

Post navigation

Next Post MonoDroid, .NET APIs For Android

About the Author

I'm interested both in solving problems with visual communication and code.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2022 Muhammad Ikhwan