zoukankan      html  css  js  c++  java
  • 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("; ", $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. // +------------------------------------------------------------------------------------------
  • 相关阅读:
    js 数组,字符串,json互相转换
    数据库相关概念
    信号量,Event, 定时器
    解决Navicat远程连接mysql很慢的方法
    Ubuntu安装mycli,让mysql命令行可以自动提示
    Requests模块调用接口
    selenium chrome浏览器对应chrome版本
    selenium 元素定位+显示等待 方法封装
    smtplib 发送文本文件和附件文件 的类方法封装
    python 数据库的方法封装
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7120684.html
Copyright © 2011-2022 走看看