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

Source: Internet
Author: User
  1. /**

  2. * Read SQL file and write to 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 \ n)) | (; \ r) ', $prefix = ', $commenter = Array (' # ', '--'))
  21. {
  22. Determine if a file exists
  23. if (!file_exists ($sqlPath))
  24. return false;
  25. $handle = fopen ($sqlPath, ' RB ');
  26. $SQLSTR = Fread ($handle, FileSize ($sqlPath));
  27. Splitting by a statement separator in SQL syntax
  28. $segment = Explode (";", Trim ($SQLSTR));
  29. Var_dump ($segment);
  30. Get rid of 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. Judging whether it would be 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. Prefix the table name
  57. if ('! = $prefix)
  58. {
  59. Only table names are valid when the first row appears, such as CREATE table Talbename
  60. $regxTable = "^[\ \" \ "]{0,1}[\_a-za-z]+[\_a-za-z0-9]*[\" \ "]{0,1}$";//Regular expression for table name processing
  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. Combining 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. 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 ', ' 123456 ', ' test ');
  161. $dbM->createfromfile (' Data.sql ', null, ' fff_ ');
  162. ?>

Copy Code
  • 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.