php 操作 mysql 实现批量执行mysql语句 mysql文件

 1 <?php
 2 /**
 3  * 批量运行sql文件
 4  * 正则分隔是重点 preg_split("/;[\r\n]+/", filecontent)
 5  */
 6 $config = require '../dataconfig.php';
 7 $dbhost = $config['DB_HOST'];
 8 $dbname = $config['DB_USER'];
 9 $dbpass = $config['DB_PWD'];
10 $db_database = $config['DB_NAME'];
11 $sqlfile = 'aa.sql'; //运行sql文件地址
12 $db = new mysqli($dbhost, $dbname, $dbpass, $db_database) or die("连接数据库失败!");
13 $db->query("set names 'utf8'");
14 $sql_array = preg_split("/;[\r\n]+/", file_get_contents($sqlfile));
15 foreach ($sql_array as $k => $v) {
16     $result = $db->query($v);
17     if ($result) {
18         echo "Query OK<br/>";
19     } else {
20         echo $db->error() . '<br>';
21     }
22 }
23 $db->close();

 

如果本文章已帮助到您!

posted @ 2018-06-30 09:03  撑一支船蒿  阅读(1067)  评论(0编辑  收藏  举报