PHP处理SQL脚本文件导入到MySQL的代码实例

添加时间:14-10-07 所属分类:PHP工具与代码
  转载自http://www.jb51.net/article/48187.htm

通常在制作安装程式,数据备份程序的时候会要用到这样的代码,我看网上有是有不太多,而且有些也不是很好用,有时候这种代码直接用现成的可以节省很多时间,那么我就从stackoverflow转了一个过来,需要的朋友可以参考下
复制代码 代码如下:
[code]<?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";
?>[/code]

前篇:济南芙蓉街和曲水亭街热闹 其他老街巷游客罕... 后篇:PHP乱码问题,UTF-8(乱码),很全的说明和...
发表我的评论


推荐文章   冷冬济南孤独老人样本调查:3个独居老人...   腌的咸菜是致癌毒物,少吃为妙   天使不放弃!无臂幼童成功用脚将食物送...   一家四口30年省吃俭用捐出600多万做公益   20年前济南人的生活才叫生活,现在只能...
随机文章   install.php简单制作   PHP中UBB的使用   mysql中engine=innodb和engine=myisam的...   基于PHP读取TXT文件向数据库导入海量数...   流行的开源PHP框架排行榜
广告

其他推荐

因为本站所有内容均转载自其它媒体,本意为公众提供免费服务,但并不代表本网赞同其观点,也不能对其真实性验证负责,如稿件版权单位或个人不想在本网发布,请与我联系,本人会立即将其撤除,谢谢.联系方式:atseashawk@163.com QQ:99289555