Re: Import .sql file via PHP
You can try something like this:
Code:
// let's pretend that connection to server is established
// and database chosen...
$sql = explode(';#%%', file_get_contents ('backup.sql'));
$n = count ($sql) - 1;
for ($i = 0; $i < $n, $i++) {
$query = $sql[$i];
$result = mysql_query ($query)
or die ('<p>Query: <br><tt>' . $query .
'</tt><br>failed. MySQL error: ' . mysql_error());
}
Note that I am using ';#%%' to break the file into queries simply because your backup script seems to insert it after each query.Normally, queries would end with alone.
Also note that the for() cycle is written so that the last member of the $sql array is not executed, since that member does not contain any SQL (there can be no SQL after the last occurrence of).
Re: Import .sql file via PHP
You may also use the following code :
Assuming each instruction is on a line.. (untested)
Code:
$db = mysql_connect(....);
mysql_select_db(....);
$fp = fopen('somefile.sql', 'r');
while($fp != feof())
{
$line = fread($fp, 2048);
$line = mysql_real_escape_string($db, $line);
mysql_query($line);
}
fclose($fp);