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();
原文地址:https://www.cnblogs.com/handle/p/9246090.html
时间: 2024-10-28 10:25:55