php运行sql文件

php7.0版本,其他版本请膝盖mysqli_* 几个函数名和参数

<?php
header("Content-Type: text/html;charset=utf-8");
	function populate_db($DBname, $DBPrefix, $sqlfile , $link ) {//配置信息
		global $errors;  
		@mysqli_select_db($link,$DBname);  
		mysqli_query($link,"SET NAMES utf8");
		$query = fread(fopen($sqlfile, "r"), filesize($sqlfile)); 
		$pieces  = split_sql($query);  
	  
		for ($i=0; $i<count($pieces); $i++) {  
			$pieces[$i] = trim($pieces[$i]);  
			if(!empty($pieces[$i]) && $pieces[$i] != "#") {  
				$pieces[$i] = str_replace( "#__", $DBPrefix, $pieces[$i]);  
				if (!$result = @mysqli_query ($link,$pieces[$i])) {  
					$errors[] = array ( mysql_error(), $pieces[$i] );  
				}  
			}  
		}  
	}  
	  
	function split_sql($sql) {  
		$sql = trim($sql);  
		$sql = str_replace("\r\n", "\n", $sql);  
	  
		$buffer = array();  
		$ret = array();  
		$in_string = false;  
	  
		for($i=0; $i<strlen($sql)-1; $i++) {  
			if($sql[$i] == ";" && !$in_string) {  
				$ret[] = substr($sql, 0, $i);  
				$sql = substr($sql, $i + 1);  
				$i = 0;  
			}  
	  
			if($in_string && ($sql[$i] == $in_string) && $buffer[1] != "\\") {  
				$in_string = false;  
			}  
			elseif(!$in_string && ($sql[$i] == '"' || $sql[$i] == "'") && (!isset($buffer[0]) || $buffer[0] != "\\")) {  
				$in_string = $sql[$i];  
			}  
			if(isset($buffer[1])) {  
				$buffer[0] = $buffer[1];  
			}  
			$buffer[1] = $sql[$i];  
		}  
	  
		if(!empty($sql)) {  
			$ret[] = $sql;  
		}  
		return($ret);  
	}
	$cfg_dbhost = '127.0.0.1:3306';
	$cfg_dbuser = 'root';
	$cfg_dbpwd = 'root';
	$cfg_db_language = 'utf8';
	$links = mysqli_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);
	  
	if (!mysqli_select_db($links,'aaa')){  
	  exit();
	}
	populate_db( 'aaa','','aaa.sql',$links );
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值