PHP reads the SQL file and imports the database (example)

Source: Internet
Author: User
PHP reads the SQL file and imports the database (example)

  1. /**

  2. * Read the SQL file and write it to the database
  3. * @ Version 1.01 demo. php
  4. * @ Author xingshaocheng
  5. * @ Edit: bbs.it-home.org
  6. */
  7. Class DBManager
  8. {
  9. Var $ dbHost = '';
  10. Var $ dbUser = '';
  11. Var $ dbPassword = '';
  12. Var $ dbSchema = '';
  13. Function _ construct ($ host, $ user, $ password, $ schema)
  14. {
  15. $ This-> dbHost = $ host;
  16. $ This-> dbUser = $ user;
  17. $ This-> dbPassword = $ password;
  18. $ This-> dbSchema = $ schema;
  19. }
  20. Function createFromFile ($ sqlPath, $ delimiter = '(; \ n) | (; \ r)', $ prefix = '', $ commenter = array ('#','--'))
  21. {
  22. // Determine whether a file exists
  23. If (! File_exists ($ sqlPath ))
  24. Return false;
  25. $ Handle = fopen ($ sqlPath, 'RB ');
  26. $ SqlStr = fread ($ handle, filesize ($ sqlPath ));
  27. // Use the statement delimiter of SQL syntax to separate
  28. $ Segment = explode (";", trim ($ sqlStr ));
  29. // Var_dump ($ segment );
  30. // Remove comments and extra blank lines
  31. Foreach ($ segment as & $ statement)
  32. {
  33. $ Sentence = explode ("\ n", $ statement );
  34. $ NewStatement = array ();
  35. Foreach ($ sentence as $ subSentence)
  36. {
  37. If (''! = Trim ($ subSentence ))
  38. {
  39. // Determine whether it is a comment
  40. $ IsComment = false;
  41. Foreach ($ commenter as $ comer)
  42. {
  43. If (eregi ("^ (". $ comer. ")", trim ($ subSentence )))
  44. {
  45. $ IsComment = true;
  46. Break;
  47. }
  48. }
  49. // If it is not a comment, it is considered an SQL statement
  50. If (! $ IsComment)
  51. $ NewStatement [] = $ subSentence;
  52. }
  53. }
  54. $ Statement = $ newStatement;
  55. }
  56. // Add a prefix to the table name
  57. If (''! = $ Prefix)
  58. {
  59. // Valid only when the TABLE name appears in the first row, for example, create table talbeName
  60. $ RegxTable = "^ [\ '\"] {0, 1} [\ _ a-zA-Z] + [_ a-zA-Z0-9] * [\' \ "] {0, 1} $ "; // process the regular expression of the table name
  61. $ RegxLeftWall = "^ [\ '\"] developed ";
  62. $ SqlFlagTree = array (
  63. "CREATE" => array (
  64. "TABLE" => array (
  65. "$ RegxTable" => 0
  66. )
  67. ),
  68. "INSERT" => array (
  69. "INTO" => array (
  70. "$ RegxTable" => 0
  71. )
  72. )
  73. );
  74. Foreach ($ segment as & $ statement)
  75. {
  76. $ Tokens = split ("", $ statement [0]);
  77. $ TableName = array ();
  78. $ This-> findTableName ($ sqlFlagTree, $ tokens, 0, $ tableName );
  79. If (empty ($ tableName ['leftwall'])
  80. {
  81. $ NewTableName = $ prefix. $ tableName ['name'];
  82. }
  83. Else {
  84. $ NewTableName = $ tableName ['leftwall']. $ prefix. substr ($ tableName ['name'], 1 );
  85. }
  86. $ Statement [0] = str_replace ($ tableName ['name'], $ newTableName, $ statement [0]);
  87. }
  88. }
  89. // Combine SQL statements
  90. Foreach ($ segment as & $ statement)
  91. {
  92. $ NewStmt = '';
  93. Foreach ($ statement as $ sentence)
  94. {
  95. $ NewStmt = $ newStmt. trim ($ sentence). "\ n ";
  96. }
  97. $ Statement = $ newStmt;
  98. }
  99. // Used for testing ------------------------
  100. // Var_dump ($ segment );
  101. // WriteArrayToFile('data.txt ', $ segment );
  102. //-------------------------------
  103. Self: saveByQuery ($ segment );
  104. Return true;
  105. }
  106. Private function saveByQuery ($ sqlArray)
  107. {
  108. $ Conn = mysql_connect ($ this-> dbHost, $ this-> dbUser, $ this-> dbPassword );
  109. Mysql_select_db ($ this-> dbSchema );
  110. Foreach ($ sqlArray as $ SQL)
  111. {
  112. Mysql_query ($ SQL );
  113. }
  114. Mysql_close ($ conn );
  115. }
  116. Private function findTableName ($ sqlFlagTree, $ tokens, $ tokensKey = 0, & $ tableName = array ())
  117. {
  118. $ RegxLeftWall = "^ [\ '\"] developed ";
  119. If (count ($ tokens) <= $ tokensKey)
  120. Return false;
  121. If (''= trim ($ tokens [$ tokensKey])
  122. {
  123. Return self: findTableName ($ sqlFlagTree, $ tokens, $ tokensKey + 1, $ tableName );
  124. }
  125. Else
  126. {
  127. Foreach ($ sqlFlagTree as $ flag => $ v)
  128. {
  129. If (eregi ($ flag, $ tokens [$ tokensKey])
  130. {
  131. If (0 = $ v)
  132. {
  133. $ TableName ['name'] = $ tokens [$ tokensKey];
  134. If (eregi ($ regxLeftWall, $ tableName ['name'])
  135. {
  136. $ TableName ['leftwall'] = $ tableName ['name'] {0 };
  137. }
  138. Return true;
  139. }
  140. Else {
  141. Return self: findTableName ($ v, $ tokens, $ tokensKey + 1, & $ tableName );
  142. }
  143. }
  144. }
  145. }
  146. Return false;
  147. }
  148. }

  149. Function writeArrayToFile ($ fileName, $ dataArray, $ delimiter = "\ r \ n ")

  150. {
  151. $ Handle = fopen ($ fileName, "wb ");
  152. $ Text = '';
  153. Foreach ($ dataArray as $ data)
  154. {
  155. $ Text = $ text. $ data. $ delimiter;
  156. }
  157. Fwrite ($ handle, $ text );
  158. }

  159. // Test

  160. $ DbM = new DBManager ('localhost', 'w01f', '000000', 'test ');
  161. $ DbM-> createFromFile ('data. SQL ', null, 'fff _');
  162. ?>

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.