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

php导入sql文件的更多相关文章

  1. Linux环境导入*.sql文件出现数据库为空

    登录mysql命令: 导入.sql文件: 或者: mysql -h127.0.0.1 -uroot -p userDb < /home/user.sql  按回车键后输数据库的密码 导入成功后, ...

  2. WampServer数据库导入sql文件

    WampServer中MySQL如何导入sql文件: http://jingyan.baidu.com/article/3c343ff7f9c7940d377963c0.html

  3. 向mysql workbench中导入.sql文件

    mysql workbench用的不多,前段时间装了一下,然后用了一下,感觉操作比dbdesigner4要更人性化一点.其中二个方面做了改进,让我觉得很爽. 第一,就是端口可以修改了,以前就是定死33 ...

  4. 命令行导入SQL文件

    摘要:把数据库导出为XX.sql格式的数据库文件,导入到另外一个数据库中的时候,总是无法全部导入.及时用mysql的命令界面导入依然是无法全部导入.老师告诉我:在命令行中运行的效率和成功率是最快和最高 ...

  5. mysql 启动 导入sql文件

    mysql mysqld.exe mysqld.exe 是mysql的服务器进程,只有先启动这个进程才能连接服务器 dos下进入mysql文件目录下的bin目录,输入mysql -u root -p ...

  6. MYSQL 编码方式 ------导入 .sql 文件 报编码错误

    在做计量泵上位机时,利用MYSQL存储数据,建表hisruninfo(计量泵历史运行数据表). 表格式为utf8,查过数据库 格式 同样为utf8 导入该hisruninfo.sql文件时,并没有报错 ...

  7. mysql导入.sql文件

    1. source /home/susie ...../**.sql 2. \. /home/susie/.../**.sql 批量导入.sql文件 首先新建一个main.sql,然后在main.sq ...

  8. MySQL导入sql 文件的5大步骤

    http://database.51cto.com/art/201005/200739.htm 以下的文章主要介绍的是MySQL导入sql 文件,即MySQL数据库导入导出sql 文件的实际操作步骤, ...

  9. mysql数据库导入sql文件时提示“Error Code: 1153 - Got a packet bigger than 'max_allowed_packet' bytes”解决办法

    向mysql数据库中导入sql文件时,如果文件过大(几百M),会提示"Error Code: 1153 - Got a packet bigger than 'max_allowed_pac ...

  10. navicat导入.sql文件

    用Navicat for Mysql导入.sql文件   虽然这算不上什么难事,但是对于新手来说(比如说我),Navicat for MySQL里的导出连接.运行SQL文件.导入向导.还原备份.这些功 ...

随机推荐

  1. Ajax实现文件的上传

    Ajax实现文件的上传 准备 ajax的参数补充 type不写的话默认是GET dataType和ContentType: dataType: 浏览器发给服务器希望返回的数据类型 .. 如果明确地指定 ...

  2. ACM_蛋糕小王子铁头娃

    Problem Description: 铁头娃制作了很多蛋糕,想分给他的小伙伴们,他的小伙伴很喜欢铁头娃做的蛋糕,每个人都想分到最多蛋糕 铁头娃想到了一个头铁主意:先给小伙伴们从1到N编号,在1-N ...

  3. UIPickerView的应用

    UIPickerView 是一个选择器控件, 它可以生成单列的选择器,也可生成多列的选择器.UIPickerView 直接继承了 UIView ,没有继承 UIControl ,因此,它不能像 UIC ...

  4. RecyclerView中item无法充满的问题

    首先致谢:https://blog.csdn.net/yuanlvmao/article/details/51694211 咱们不是代码的生产者,只是代码的搬运工. 今天写了一个RecyclerVie ...

  5. Android彻底组件化demo发布

    今年6月份开始,我开始负责对"得到app"的android代码进行组件化拆分,在动手之前我查阅了很多组件化或者模块化的文章,虽然有一些收获,但是很少有文章能够给出一个整体且有效的方 ...

  6. 顺序表查找及其优化(Java)

    顺序表查找(线性查找): private static void Ordersearch(int[] arr,int num) { for (int i = 0; i < arr.length; ...

  7. struts2.5.2 通配符问题_亲测有用

    学了一段时间struts2,跟着教程做,但发现struts2的版本不同,很多东西的使用是有差异的.例如之前遇到的创建sessionFactory的方式就跟之前版本有着明显的差异.今天又遇到一个问题,那 ...

  8. UICollectionViewFlowLayout & UICollectionViewDelegateFlowLayout

    A concrete layout object that organizes items into a grid with optional header and footer views for ...

  9. Html test

    <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  10. linux搭建mysql服务器及可视化工具

    环境: ubutnu 18.4 mysql 5.7 参考: 安装 https://www.cnblogs.com/opsprobe/p/9126864.html 配置用户权限 https://baij ...