PHP处理SQL脚本文件导入到MySQL的代码实例
前端之家收集整理的这篇文章主要介绍了
PHP处理SQL脚本文件导入到MySQL的代码实例,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<div class="codetitle"><a style="CURSOR: pointer" data="79607" class="copybut" id="copybut79607" onclick="doCopy('code79607')"> 代码如下:
<div class="codebody" id="code79607"><?
PHP// Name of the file
$filename = 'churc.
sql';
//
MysqL host
$
MysqL_host = 'localhost';
//
MysqL username
$
MysqL_username = 'root';
//
MysqL password
$
MysqL_password = '';
// Database name
$
MysqL_database = 'dump';// Connect to
MysqL server
MysqL_connect($
MysqL_host,$
MysqL_username,$
MysqL_password) or die('Error connecting to
MysqL server: ' .
MysqL_error());
// Select database
MysqL_select_db($
MysqL_database) or die('Error selecting
MysqL database: ' .
MysqL_error());// Temporary variable,used to store current query
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line)
{
// Skip it if it's a comment
if (substr($line,2) == '--' || $line == '')
continue;// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end,it's the end of the query
if (substr(trim($line),-1,1) == ';')
{
// Perform the query
MysqL_query($templine) or print('Error performing query \'
' . $templine . '\': ' . MysqL_error() . '
');
// Reset temp variable to empty
$templine = '';
}
}
echo "Tables imported successfully";
?>