Just wanna to add to @Bill Karwin answer given above.
You can import | reinitialize | execute custom SQL; the database using sql script file, by simply clicking on button. That button would execute the sql script file using ajax.
eg.
Front end code
<input type="button" value="Execute SQL Script" id="btnExecuteScript" />
<input type="button" value="reset" onclick="clearDiv('divExecuteScript')" />
<div id="divExecuteScript" style='display: none'></div>
<br />
Jquery code calling the ajax
$('#btnExecuteScript').click(function (event) {
if ($('#divExecuteScript').html() == '') {
$('#divExecuteScript').html("<b style='font-family: sans-serif;font-size: larger'>Please Wait, It might take a few minutes</b>");
$('#divExecuteScript').show();
$.get("../controller/Controller.php?executeScript=TRUE", function (data) {
// alert("$" + data + "$");
$('body').css('cursor', 'default');
$('#divExecuteScript').html(data);
$('#divExecuteScript').show();
});
} else
$('#divExecuteScript').toggle();
});
connection file
class Conn {
protected $databaseURL; // const
protected $databaseName;
protected $databaseUName;
protected $databasePWord;
public $mysqli;
public function __construct($args = null) {
if (stripos($_SERVER['SERVER_NAME'], "localhost") !== FALSE) {
$this->databaseURL = "host";
$this->databaseName = "database";
$this->databaseUName = "user";
$this->databasePWord = "password";
}
$this->mysqli = new mysqli($this->databaseURL, $this->databaseUName, $this->databasePWord, $this->databaseName) or die('Could not connect to the database server' . mysqli_connect_error());
if (empty($this->mysqli))
die("Error while connecting to host");
}
function get_databaseURL() {
return $this->databaseURL;
}
function get_databaseUName() {
return $this->databaseUName;
}
function get_databasePWord() {
return $this->databasePWord;
}
function get_databaseName() {
return $this->databaseName;
}
}
controller code executing the command
$con = new Conn();
$mysqli = new mysqli($con->get_databaseURL(), $con->get_databaseUName(), $con->get_databasePWord(), $con->get_databaseName()) or die('Could not connect to the database server' . mysqli_connect_error());
if (isset($_GET['executeScript'])) {
$script_path = '/path-to-script-file/filename.sql';
$command = "mysql --user={$con->get_databaseUName()} --password='{$con->get_databasePWord()}' "
. "-h {$con->get_databaseURL()} -D {$con->get_databaseName()} < {$script_path}";
$output = shell_exec($command);
if (!empty($output))
echo "<b style='font-family: sans-serif;font-size: large'>Execute the SQL script<br />";
else
echo "<b style='font-family: sans-serif;font-size: large'>Unable to execute the SQL script</b><br />";
return;
}
shell_exec()
andexec()
is thatshell_exec
returns all of the output stream as a string.exec
returns the last line of the output. via stackoverflow.com/a/7093892/557358