php導入sql文件


php導入sql文件

sql php


php導入sql文件

基本思路

1.打開sql文件,放入一個變量(字符串類型)其中

2.使用正則替換掉其中的凝視(“--”與“/**/”)

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

4.鏈接數據庫之后使用my_query()運行sql

代碼

    
    
   
   
           
  1. <?php
  2. // +------------------------------------------------------------------------------------------
  3. // | Author: longDD <longdd_love@163.com>
  4. // +------------------------------------------------------------------------------------------
  5. // | There is no true,no evil,no light,there is only power.
  6. // +------------------------------------------------------------------------------------------
  7. // | Description: import sql Dates: 2014-08-07
  8. // +------------------------------------------------------------------------------------------
  9. class ImportSql
  10. {
  11. /** @var $content 數據庫連接 */
  12. protected $connect = null;
  13. /** @var $db 數據庫對象 */
  14. protected $db = null;
  15. /** @var $sqlFile sql文件 */
  16. public $sqlFile = "";
  17. /** @array @sqlArr sql語句數組 */
  18. public $sqlArr = array();
  19. /**
  20. * 構造函數
  21. *
  22. * @param string $host 主機地址
  23. * @param string $user username
  24. * @param string $pw password
  25. * @param $db_name 數據庫名稱
  26. * @return void
  27. */
  28. public function __construct($host, $user, $pw, $db_name)
  29. {
  30. /** 連接數據庫 */
  31. $this->connect = mysql_connect($host, $user, $pw) or die("Could not connect: " . mysql_error());
  32. /** 選中數據庫 */
  33. $this->db = mysql_select_db($db_name, $this->connect) or die("Yon can not select the table:" . mysql_error());
  34. }
  35. /**
  36. * 導入sql文件
  37. *
  38. * @param string $url 文件路徑
  39. * @return true 導入成返回true
  40. */
  41. public function Import($url)
  42. {
  43. if(!file_exists($url))
  44. {
  45. exit("文件不存在!

    ");

  46. }
  47. $this->sqlFile = file_get_contents($url);
  48. if (!$this->sqlFile)
  49. {
  50. exit("打開文件錯誤!

    ");

  51. }
  52. else
  53. {
  54. $this->GetSqlArr();
  55. if ($this->Runsql())
  56. {
  57. return true;
  58. }
  59. }
  60. }
  61. /**
  62. * 獲取sql語句數組
  63. *
  64. * @return void
  65. */
  66. public function GetSqlArr()
  67. {
  68. /** 去除凝視 */
  69. $str = $this->sqlFile;
  70. $str = preg_replace('/--.*/i', '', $str);
  71. $str = preg_replace('/\/\*.*\*\/(\;)?/i', '', $str);
  72. /** 去除空格 創建數組 */
  73. $str = explode(";\n", $str);
  74. foreach ($str as $v)
  75. {
  76. $v = trim($v);
  77. if (empty($v))
  78. {
  79. continue;
  80. }
  81. else
  82. {
  83. $this->sqlArr[] = $v;
  84. }
  85. }
  86. }
  87. /**
  88. * 運行sql文件
  89. *
  90. * @return true 運行成功返回true
  91. */
  92. public function RunSql()
  93. {
  94. /** 開啟事務 */
  95. if (mysql_query('BEGIN'))
  96. {
  97. foreach ($this->sqlArr as $k => $v)
  98. {
  99. if (!mysql_query($v))
  100. {
  101. /** 回滾 */
  102. mysql_query('ROLLBACK');
  103. exit("sql語句錯誤:第" . $k . "行" . mysql_error());
  104. }
  105. }
  106. /** 提交事務 */
  107. mysql_query('COMMIT');
  108. return true;
  109. }
  110. else
  111. {
  112. exit('無法開啟事務!

    ');

  113. }
  114. }
  115. }
  116. // +------------------------------------------------------------------------------------------
  117. // | End of ImportSql class
  118. // +------------------------------------------------------------------------------------------
  119. /**
  120. * This is a example.
  121. */
  122. header("Content-type:text/html;charset=utf-8");
  123. $sql = new ReadSql("localhost", "root", "", "log_db");
  124. $rst = $sql->Import("./log_db.sql");
  125. if ($rst)
  126. {
  127. echo "Success。";
  128. }
  129. // +------------------------------------------------------------------------------------------
  130. // | End of file ImportSql.php
  131. // +------------------------------------------------------------------------------------------
  132. // | Location: ./ImportSql.php
  133. // +------------------------------------------------------------------------------------------


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM