Snippet Name: Import SQL Data
Description: This script will create tables and load data from an sql file.
Comment: (none)
Language: PHP
Highlight Mode: PHP
Last Modified: February 27th, 2009
|
<?PHP
FUNCTION run_query_batch($handle, $filename=""){
// Open SQL file.
IF (! ($fd = FOPEN($filename, "r")) ) {
DIE("Failed to open $filename: " . mysql_error() . "<br>");
}
// Iterate through each line in the file.
WHILE (!FEOF($fd)) {
// Read next line from file.
$line = FGETS($fd, 32768);
$stmt = "$stmt$line";
// Semicolon indicates end of statement, keep adding to the statement.
// until one is reached.
IF (!PREG_MATCH("/;/", $stmt)) {
CONTINUE;
}
// Remove semicolon and execute entire statement.
$stmt = PREG_REPLACE("/;/", "", $stmt);
// Execute the statement.
mysql_query($stmt, $handle) ||
DIE("Query failed: " . mysql_error() . "<br>");
$stmt = "";
}
// Close SQL file.
FCLOSE($fd);
}
run_query_batch($dbhandle, "schema.sql");
?> |