db_insert_wordindex_from_csv.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. require_once "install_head.php";
  3. include "./_pdo.php";
  4. ?>
  5. <!DOCTYPE html>
  6. <html>
  7. <head>
  8. </head>
  9. <body>
  10. <h2>Insert to Index</h2>
  11. <?php
  12. if (isset($_GET["from"]) == false) {
  13. $from = 0;
  14. } else {
  15. $from = $_GET["from"];
  16. }
  17. $dirLog = _DIR_LOG_ . "/";
  18. $filelist = array();
  19. $fileNums = 0;
  20. $log = "";
  21. echo "<h2>doing : No.{$from} </h2>";
  22. global $dbh_word_index;
  23. $dns = _FILE_DB_WORD_INDEX_;
  24. $dbh_word_index = new PDO($dns, _DB_USERNAME_, _DB_PASSWORD_, array(PDO::ATTR_PERSISTENT => true));
  25. $dbh_word_index->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  26. if (($fpoutput = fopen(_DIR_CSV_PALI_CANON_WORD_INDEX_ . "/{$from}.csv", "r")) !== false) {
  27. // 开始一个事务,关闭自动提交
  28. $dbh_word_index->beginTransaction();
  29. $query = "INSERT INTO "._TABLE_WORD_INDEX_." (id , word , word_en , count , normal , bold , is_base , len ) VALUES (?,?,?,?,?,?,?,?)";
  30. $stmt = $dbh_word_index->prepare($query);
  31. $count = 0;
  32. while (($data = fgetcsv($fpoutput, 0, ',')) !== false) {
  33. $stmt->execute($data);
  34. $count++;
  35. }
  36. // 提交更改
  37. $dbh_word_index->commit();
  38. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  39. $error = $dbh_word_index->errorInfo();
  40. echo "error - $error[2] <br>";
  41. $log .= "$from, $FileName, error, $error[2] \r\n";
  42. } else {
  43. echo "updata $count recorders.<br />";
  44. $log .= "updata $count recorders.\r\n";
  45. }
  46. } else {
  47. echo "<h2>齐活!功德无量!all done!</h2>";
  48. exit;
  49. }
  50. echo "<script>";
  51. echo "window.location.assign(\"db_insert_wordindex_from_csv.php?from=" . ($from + 1) . "\")";
  52. echo "</script>";
  53. echo "正在载入:" . ($from + 1) . "——" . $filelist[$from + 1][0];
  54. ?>
  55. </body>
  56. </html>