updatesql.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. class updatesql_c extends Control
  3. {
  4. function __construct()
  5. {
  6. parent::Control();
  7. }
  8. function updatesql_c()
  9. {
  10. $this->__construct();
  11. }
  12. function index_f()
  13. {
  14. // $this->createsql();
  15. echo "数据库升级程序";
  16. }
  17. function update_f()
  18. {
  19. //include_once("./install/global.php");
  20. //require_once("./install/global.php");
  21. //print_r($this->db->host."ok");
  22. /*$host=$this->db->host;
  23. $user=$this->db->user;
  24. $password=$this->db->pass;
  25. $dbname=$this->db->data;*/
  26. //header("Access-Control-Allow-Origin:*");
  27. if(!file_exists("../update.sql"))
  28. {
  29. echo "缺少数据库更新文件:update.sql,请检查!";
  30. exit;
  31. }
  32. //更新数据库
  33. $sql = file_get_contents("../update.sql");
  34. $this->run_sql($sql);
  35. echo "数据库升级成功";
  36. exit;
  37. }
  38. function run_sql($sql)
  39. {
  40. $sql = str_replace("\r","\n",$sql);
  41. $ret = array();
  42. $num = 0;
  43. foreach(explode(";\n", trim($sql)) as $query) {
  44. $queries = explode("\n", trim($query));
  45. foreach($queries as $query) {
  46. $ret[$num] .= $query[0] == '#' || $query[0].$query[1] == '--' ? '' : $query;
  47. }
  48. $num++;
  49. }
  50. unset($sql);
  51. //print_r($ret);
  52. //exit;
  53. foreach($ret as $query) {
  54. $query = trim($query);
  55. if($query) {
  56. $this->db->query($query);
  57. //兼容 mysql 4.1
  58. /*if(substr($query, 0, 12) == 'CREATE TABLE') {
  59. $this->db->query($this->create_table($query));
  60. } else {
  61. $this->db->query($query);
  62. }*/
  63. }
  64. }
  65. }
  66. //兼容 mysql 4.1
  67. function create_table($sql)
  68. {
  69. return preg_replace("/^\s*(CREATE TABLE\s+.+\s+\(.+?\)).*$/isU", "\\1", $sql).(mysql_get_server_info() > '4.1' ? " ENGINE=MyISAM DEFAULT CHARSET=utf8" : " TYPE=MYISAM");
  70. }
  71. }
  72. ?>