Tuesday, March 3, 2009

Run a .SQL file from PHP

Execute a .SQL file from PHP code

// executes the SQL commands from an external file.

$file = "sqlQueryfile.sql";
//Check if file exists
if (!file_exists($file))
{
die("Error : "The file $file does not exist.");
}

$str = file_get_contents($file);
if (!$str)
{
die("Error : Unable to read the contents of $file.");
}

$this->last_query = $str;

// split all the query's into an array
$sql = explode(';', $str);
foreach ($sql as $query)
{
if (!empty($query))
{
$r = mysql_query($query);
if (!$r)
{
die("Error : "mysql_error());

}
}
}

No comments:

Post a Comment

Express you views