PHP处理SQL脚本文件导入到MySQL的代码实例
2015-01-24信息快讯网
通常在制作安装程式,数据备份程序的时候会要用到这样的代码,我看网上有是有不太多,而且有些也不是很好用,有时候这种代码直接用现成的可以节省很多时间,那么我就从stackoverflow转了一个过来,需要的朋友可以参考下
<?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";
?>PHP中数据库单例模式的实现代码分享
PHP实现的多彩标签效果代码分享
PHP中几个可以提高运行效率的代码写法、技巧分享
PHP中使用sleep造成mysql读取失败的案例和解决方法
从零开始学YII2框架(五)快速生成代码工具 Gii 的使用
PHP中使用localhost连接Mysql不成功的解决方法
php实现文件下载代码分享
win7 64位系统 配置php最新版开发环境(php+Apache+mysql)
Php连接及读取和写入mysql数据库的常用代码
PHP连接sql server 2005环境配置及问题解决
ThinkPHP实现将SESSION存入MYSQL的方法
PHP读取文件内容后清空文件示例代码
PHP中CURL的CURLOPT_POSTFIELDS参数使用细节
PHP同时连接多个mysql数据库示例代码
19个超实用的PHP代码片段
使用PHP导出Redis数据到另一个Redis中的代码
PHP连接MySQL的2种方法小结以及防止乱码
php调用C代码的实现方法
php设置允许大文件上传示例代码
php操作mysql数据库的基本类代码
php实现mysql数据库操作类分享
thinkphp3查询mssql数据库乱码解决方法分享
基于php和mysql的简单的dao类实现crud操作功能