PHP处理SQL脚本文件导入到MySQL的代码实例
2014-03-17 13:50
246 查看
<?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, 0, 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 \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
// Reset temp variable to empty
$templine = '';
}
}
echo "Tables imported successfully";
?>
// 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, 0, 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 \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
// Reset temp variable to empty
$templine = '';
}
}
echo "Tables imported successfully";
?>
相关文章推荐
- PHP处理SQL脚本文件导入到MySQL的代码实例
- PHP导入导出mysql为sql文件的方法(已经验证,有实例,很好用)
- mysql导入sql脚本文件的正确姿势
- Mysql导入教大sql脚本文件
- PHP按行读取、处理较大CSV文件的代码实例
- java代码实现mysql的sql文件导入
- mysql导入过大.sql文件报错处理
- PHP按行读取、处理较大CSV文件的代码实例
- Mysql导入大尺寸sql脚本文件
- mysql导入sql文件时乱码问题处理
- php+mysql将大数据sql文件导入数据库
- bat脚本执行mysql命令导入sql文件到数据库
- MySQL导入的sql脚本文件过大解决方案
- PHP执行.SQL文件的实例代码分享
- windows导入sql脚本文件报错(mysql)
- mysql-导入导出sql脚本文件
- 在只有MySQL数据库的情况下,如何把SQL Server 数据脚本(.sql文件)导入Mysql的表中。
- Mysql导入大容量SQL文件数据问题
- mysql&nbsp;使用命令行导入导出sql脚本
- Windows平台导出导入sql文件Mysql表名变小写的解决(已测试)