PHP Import SQL file _php tutorial

Source: Internet
Author: User

PHP Import SQL file


PHP Import SQL file

sqlphp

PHP Import SQL file

Basic ideas

1. Open the SQL file and put it into a variable (string type)

2. Use regular to replace comments ("--" and "/**/")

3. Use explode to split into an array and strip each line of space

4. Using My_query () to execute SQL after linking the database

Code

 
 
  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.html www.bkjia.com true http://www.bkjia.com/phpjc/871202.html Techarticle php Import SQL file PHP import SQL file sql PHP PHP Import SQL file basic idea 1. Open the SQL file and put it into a variable (string type) 2. Use regular to replace comments in ...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.