php匯入sql檔案_PHP教程

來源:互聯網
上載者:User

php匯入sql檔案


php匯入sql檔案

sql php

php匯入sql檔案

基本思路

1.開啟sql檔案,放入一個變數(字串類型)當中

2.使用正則替換掉當中的注釋(“--”與“/**/”)

3.使用explode分割成為一個數組並去除每行的空格

4.連結資料庫之後使用my_query()執行sql

代碼

 
  1. // +------------------------------------------------------------------------------------------
  2. // | Author: longDD
  3. // +------------------------------------------------------------------------------------------
  4. // | There is no true,no evil,no light,there is only power.
  5. // +------------------------------------------------------------------------------------------
  6. // | Description: import sql Dates: 2014-08-07
  7. // +------------------------------------------------------------------------------------------
  8. class ImportSql
  9. {
  10. /** @var $content 資料庫連接 */
  11. protected $connect = null;
  12. /** @var $db 資料庫物件 */
  13. protected $db = null;
  14. /** @var $sqlFile sql檔案 */
  15. public $sqlFile = "";
  16. /** @array @sqlArr sql語句數組 */
  17. public $sqlArr = array();
  18. /**
  19. * 建構函式
  20. *
  21. * @param string $host 主機地址
  22. * @param string $user 使用者名稱
  23. * @param string $pw 密碼
  24. * @param $db_name 資料庫名稱
  25. * @return void
  26. */
  27. public function __construct($host, $user, $pw, $db_name)
  28. {
  29. /** 串連資料庫 */
  30. $this->connect = mysql_connect($host, $user, $pw) or die("Could not connect: " . mysql_error());
  31. /** 選中資料庫 */
  32. $this->db = mysql_select_db($db_name, $this->connect) or die("Yon can not select the table:" . mysql_error());
  33. }
  34. /**
  35. * 匯入sql檔案
  36. *
  37. * @param string $url 檔案路徑
  38. * @return true 匯入成返回true
  39. */
  40. public function Import($url)
  41. {
  42. if(!file_exists($url))
  43. {
  44. exit("檔案不存在!");
  45. }
  46. $this->sqlFile = file_get_contents($url);
  47. if (!$this->sqlFile)
  48. {
  49. exit("開啟檔案錯誤!");
  50. }
  51. else
  52. {
  53. $this->GetSqlArr();
  54. if ($this->Runsql())
  55. {
  56. return true;
  57. }
  58. }
  59. }
  60. /**
  61. * 擷取sql語句數組
  62. *
  63. * @return void
  64. */
  65. public function GetSqlArr()
  66. {
  67. /** 去除注釋 */
  68. $str = $this->sqlFile;
  69. $str = preg_replace('/--.*/i', '', $str);
  70. $str = preg_replace('/\/\*.*\*\/(\;)?/i', '', $str);
  71. /** 去除空格 建立數組 */
  72. $str = explode(";\n", $str);
  73. foreach ($str as $v)
  74. {
  75. $v = trim($v);
  76. if (empty($v))
  77. {
  78. continue;
  79. }
  80. else
  81. {
  82. $this->sqlArr[] = $v;
  83. }
  84. }
  85. }
  86. /**
  87. * 執行sql檔案
  88. *
  89. * @return true 執行成功返回true
  90. */
  91. public function RunSql()
  92. {
  93. /** 開啟事務 */
  94. if (mysql_query('BEGIN'))
  95. {
  96. foreach ($this->sqlArr as $k => $v)
  97. {
  98. if (!mysql_query($v))
  99. {
  100. /** 復原 */
  101. mysql_query('ROLLBACK');
  102. exit("sql語句錯誤:第" . $k . "行" . mysql_error());
  103. }
  104. }
  105. /** 提交事務 */
  106. mysql_query('COMMIT');
  107. return true;
  108. }
  109. else
  110. {
  111. exit('無法開啟事務!');
  112. }
  113. }
  114. }
  115. // +------------------------------------------------------------------------------------------
  116. // | End of ImportSql class
  117. // +------------------------------------------------------------------------------------------
  118. /**
  119. * This is a example.
  120. */
  121. header("Content-type:text/html;charset=utf-8");
  122. $sql = new ReadSql("localhost", "root", "", "log_db");
  123. $rst = $sql->Import("./log_db.sql");
  124. if ($rst)
  125. {
  126. echo "Success!";
  127. }
  128. // +------------------------------------------------------------------------------------------
  129. // | End of file ImportSql.php
  130. // +------------------------------------------------------------------------------------------
  131. // | Location: ./ImportSql.php
  132. // +------------------------------------------------------------------------------------------
            • http://www.bkjia.com/PHPjc/871202.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/871202.htmlTechArticlephp匯入sql檔案 php匯入sql檔案 sql php php匯入sql檔案 基本思路 1.開啟sql檔案,放入一個變數(字串類型)當中 2.使用正則替換掉當中的注釋...

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.