123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- class updatesql_c extends Control
- {
- function __construct()
- {
- parent::Control();
- }
- function updatesql_c()
- {
- $this->__construct();
- }
- function index_f()
- {
- // $this->createsql();
- echo "数据库升级程序";
- }
-
- function update_f()
- {
- //include_once("./install/global.php");
- //require_once("./install/global.php");
- //print_r($this->db->host."ok");
- /*$host=$this->db->host;
- $user=$this->db->user;
- $password=$this->db->pass;
- $dbname=$this->db->data;*/
- //header("Access-Control-Allow-Origin:*");
-
- if(!file_exists("../update.sql"))
- {
- echo "缺少数据库更新文件:update.sql,请检查!";
- exit;
- }
- //更新数据库
- $sql = file_get_contents("../update.sql");
- $this->run_sql($sql);
- echo "数据库升级成功";
- exit;
- }
- function run_sql($sql)
- {
-
- $sql = str_replace("\r","\n",$sql);
- $ret = array();
- $num = 0;
-
- foreach(explode(";\n", trim($sql)) as $query) {
- $queries = explode("\n", trim($query));
- foreach($queries as $query) {
- $ret[$num] .= $query[0] == '#' || $query[0].$query[1] == '--' ? '' : $query;
- }
- $num++;
- }
- unset($sql);
- //print_r($ret);
- //exit;
- foreach($ret as $query) {
- $query = trim($query);
- if($query) {
- $this->db->query($query);
- //兼容 mysql 4.1
- /*if(substr($query, 0, 12) == 'CREATE TABLE') {
- $this->db->query($this->create_table($query));
- } else {
- $this->db->query($query);
- }*/
- }
- }
- }
-
- //兼容 mysql 4.1
- function create_table($sql)
- {
- return preg_replace("/^\s*(CREATE TABLE\s+.+\s+\(.+?\)).*$/isU", "\\1", $sql).(mysql_get_server_info() > '4.1' ? " ENGINE=MyISAM DEFAULT CHARSET=utf8" : " TYPE=MYISAM");
- }
- }
- ?>
|