PHP写的执行sql文件

 

<?php
define('INSTALL_PATH',dirname(__FILE__).DIRECTORY_SEPARATOR);
include '../library/base.php';
$dbfile='install.sql';


if(file_exists(INSTALL_PATH.$dbfile)){
	$mysql = base::load_sys_class('mysql');
	$sql = file_get_contents(INSTALL_PATH.$dbfile);
	$sqls = fnParseSql($sql);
	unset($sql);
	foreach($sqls as $sql){
		$mysql->query($sql); // 执行sql语句
	}
}

function fnParseSql($sql=''){
	$sql = str_replace("\r","\n",$sql);
	$ret = array();
	$aSql = explode(";\n",trim($sql));
	unset($sql);
	$num = 0;
	foreach($aSql as $sql){
		$ret[$num]='';
		$queries = explode("\n",$sql);
		$queries = array_filter($queries);
		foreach($queries as $query){
			$str1 = substr($query,0,1);
			if($str1!=='#' && $str1!=='-') $ret[$num].=$query;
		}
		$num++;
	}
	return $ret;
}

 

 

 

你可能感兴趣的:(sql,mysql,PHP)