PHP头条
热点:

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. // +------------------------------------------------------------------------------------------

相关文章

PHP之友评论

今天推荐