demo php:
复制代码 代码如下: <?php /** * 读取 sql 文件并写入数据库 * @version demo php */ class DBManager { var $dbHost = var $dbUser = var $dbPassword = var $dbSchema = function __construct($host $user $password $schema) { $this >dbHost = $host $this >dbUser = $user $this >dbPassword = $password $this >dbSchema = $schema } function createFromFile($sqlPath $delimiter = (/n)|((/r/n))|(/r) $prefix = $menter = array( # )) { //判断文件是否存在 if(!file_exists($sqlPath)) return false $handle = fopen($sqlPath rb ) $sqlStr = fread($handle filesize($sqlPath)) //通过sql语法的语句分割符进行分割 $segment = explode("" trim($sqlStr)) //var_dump($segment) //去掉注释和多余的空行 foreach($segment as &$statement) { $sentence = explode("/n" $statement) $newStatement = array() foreach($sentence as $subSentence) { if( != trim($subSentence)) { //判断是会否是注释 $isComment = false foreach($menter as $er) { if(eregi("^(" $er ")" trim($subSentence))) { $isComment = true break } } //如果不是注释 则认为是sql语句 if(!$isComment) $newStatement[] = $subSentence } } $statement = $newStatement } //对表名加前缀 if( != $prefix) { //只有表名在第一行出现时才有效 例如 CREATE TABLE talbeName $regxTable = "^[/`/ /"]{ }[/_a zA Z]+[/_a zA Z ]*[/`/ /"]{ }$"//处理表名的正则表达式 $regxLeftWall = "^[/`/ /"]{ }" $sqlFlagTree = array( "CREATE" =>array( "TABLE" =>array( "$regxTable" => ) ) "INSERT" =>array( "INTO" =>array( "$regxTable" => ) ) ) foreach($segment as &$statement) { $tokens = split(" " $statement[ ]) $tableName = array() $this >findTableName($sqlFlagTree $tokens $tableName) if(empty($tableName[ leftWall ])) { $newTableName = $prefix $tableName[ name ] } else{ $newTableName = $tableName[ leftWall ] $prefix substr($tableName[ name ] ) } $statement[ ] = str_replace($tableName[ name ] $newTableName $statement[ ]) } } //组合sql语句 foreach($segment as &$statement) { $newStmt = foreach($statement as $sentence) { $newStmt = $newStmt trim($sentence) "/n" } $statement = $newStmt } //用于测试 //var_dump($segment) //writeArrayToFile( data txt $segment) // self::saveByQuery($segment) return true } private function saveByQuery($sqlArray) { $conn = mysql_connect($this >dbHost $this >dbUser $this >dbPassword) mysql_select_db($this >dbSchema) foreach($sqlArray as $sql) { mysql_query($sql) } mysql_close($conn) } private function findTableName($sqlFlagTree $tokens $tokensKey= &$tableName = array()) { $regxLeftWall = "^[/`/ /"]{ }" if(count($tokens)<=$tokensKey) return false if( == trim($tokens[$tokensKey])) { return self::findTableName($sqlFlagTree $tokens $tokensKey+ $tableName) } else { foreach($sqlFlagTree as $flag =>$v) { if(eregi($flag $tokens[$tokensKey])) { if( ==$v) { $tableName[ name ] = $tokens[$tokensKey] if(eregi($regxLeftWall $tableName[ name ])) { $tableName[ leftWall ] = $tableName[ name ]{ } } return true } else{ return self::findTableName($v $tokens $tokensKey+ &$tableName) } } } } return false } } function writeArrayToFile($fileName $dataArray $delimiter="/r/n") { $handle=fopen($fileName "wb") $text = foreach($dataArray as $data) { $text = $text $data $delimiter } fwrite($handle $text)} //测试 $dbM = new DBManager( localhost w f test )$dbM >createFromFile( data sql null fff_ )?>lishixinzhi/Article/program/PHP/201311/21281方法:
1、首页你需要开启apache服务器;
2、然后把要运行的文件放入apache的根目录;
3、如果安装在C盘的话,就是在这个文件夹下C:\wamp\www;
4、还有要有数据的话,打开apache后将数据导入到数据库中。
在PHP中调用外部命令,可以用如下三种方法来实现:
1、用PHP提供的专门函数
(1)PHP提供共了3个专门的执行外部命令的函数:system(),exec(),passthru()。
(2)system() 原型:string system (string command [, int return_var])
(3)system()函数很其它语言中的差不多,它执行给定的命令,输出和返回结果。第二个参数是可选的,用来得到命令执行后的状态码。
2、例子:
代码如下:
<?phpsystem("/usr/local/bin/webalizer/webalizer")
?>
exec()
(1)原型:string exec (string command [, string array [, int return_var]]) exec
(2)()函数与system()类似,也执行给定的命令,但不输出结果,而是返回结果的最后一行。虽然它只返回命令结果的最后一行,但用第二个参数array 可以得到完整的结果,方法是把结果逐行追加到array的结尾处。所以如果array不是空的,在调用之前最好用unset()最它清掉。只有指定了第二 个参数时,才可以用第三个参数,用来取得命令执行的状态码。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)