Php import SQL file _ PHP Tutorial

Source: Internet
Author: User
Php imports SQL files. Php import SQL file sqlphpphp import SQL file basic idea 1. open the SQL file and place it in a variable (string type). 2. replace the comments in regular expressions with those in php to import SQL files.
Import SQL files using php

sql php

Basic idea of importing SQL files using php

1. open the SQL file and place it in a variable (string type ).

2. replace the comments ("--" and "/**/") with regular expressions.

3. use explode to split an array and remove spaces in each row

4. use my_query () to execute the SQL statement after connecting to 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 Database Connection */
  11. protected $connect = null;
  12. /** @ Var $ db database object */
  13. protected $db = null;
  14. /** @ Var $ sqlFile SQL file */
  15. public $sqlFile = "";
  16. /** @ Array @ sqlArr SQL statement array */
  17. public $sqlArr = array();
  18. /**
  19. * Constructor
  20. *
  21. * @ Param string $ host address
  22. * @ Param string $ user username
  23. * @ Param string $ pw password
  24. * @ Param $ db_name database name
  25. * @return void
  26. */
  27. public function __construct($host, $user, $pw, $db_name)
  28. {
  29. /** Connect to the database */
  30. $this->connect = mysql_connect($host, $user, $pw) or die("Could not connect: " . mysql_error());
  31. /** Select database */
  32. $this->db = mysql_select_db($db_name, $this->connect) or die("Yon can not select the table:" . mysql_error());
  33. }
  34. /**
  35. * Import an SQL file
  36. *
  37. * @ Param string $ url file path
  38. * @ Return true import to return true
  39. */
  40. public function Import($url)
  41. {
  42. if(!file_exists($url))
  43. {
  44. Exit ("The file does not exist! ");
  45. }
  46. $this->sqlFile = file_get_contents($url);
  47. if (!$this->sqlFile)
  48. {
  49. Exit ("An error occurred while opening the file! ");
  50. }
  51. else
  52. {
  53. $this->GetSqlArr();
  54. if ($this->Runsql())
  55. {
  56. return true;
  57. }
  58. }
  59. }
  60. /**
  61. * Obtain the SQL statement array
  62. *
  63. * @return void
  64. */
  65. public function GetSqlArr()
  66. {
  67. /** Remove comments */
  68. $str = $this->sqlFile;
  69. $str = preg_replace('/--.*/i', '', $str);
  70. $str = preg_replace('/\/\*.*\*\/(\;)?/i', '', $str);
  71. /** Remove spaces to create an array */
  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. * Execute the SQL file
  88. *
  89. * @ Return true: Returns true if execution is successful.
  90. */
  91. public function RunSql()
  92. {
  93. /** Start transaction */
  94. if (mysql_query('BEGIN'))
  95. {
  96. foreach ($this->sqlArr as $k => $v)
  97. {
  98. if (!mysql_query($v))
  99. {
  100. /** Rollback */
  101. mysql_query('ROLLBACK');
  102. Exit ("SQL statement error:". $ k. "row". mysql_error ());
  103. }
  104. }
  105. /** Submit the transaction */
  106. mysql_query('COMMIT');
  107. return true;
  108. }
  109. else
  110. {
  111. Exit ('the transaction cannot be started! ');
  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. // +------------------------------------------------------------------------------------------
            • Using php to import SQL files SQL php to import SQL files Basic idea 1. open the SQL file and put it into a variable (string type) 2. replace the comment with regular expressions...

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.