Advertisement
Guest User

Untitled

a guest
Nov 4th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. #!/usr/bin/php -q
  2.  
  3. <?php
  4.  
  5. if ($_SERVER["argc"] > 1 && file_exists($_SERVER["argv"][1]) === TRUE) {
  6. } else {
  7. echo "USAGE: php cities-import.php input-file\n";
  8. $dbh = NULL;
  9. exit;
  10. }
  11.  
  12. $dbName = readline("Database Name: ");
  13.  
  14. if (strlen($dbName) < 1) {
  15. echo "\nERROR: Database name required.\n";
  16. $dbh = NULL;
  17. exit;
  18. }
  19.  
  20. $dbUser = readline("Database User: ");
  21.  
  22. if (strlen($dbUser) < 1) {
  23. echo "\nERROR: Database user required.\n";
  24. $dbh = NULL;
  25. exit;
  26. }
  27.  
  28. $dbPass = readline("Database Password: ");
  29. $dbh = new PDO('mysql:host=localhost;dbname='.$dbName, $dbUser, $dbPass);
  30. $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  31. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  32. $index = array();
  33. $sql = "SELECT city, state_or_province FROM isvr_cities";
  34. $sth = $dbh->query($sql);
  35.  
  36. foreach ($sth as $row) {
  37. $index[] = $row['city'].$row['state_or_province'];
  38. }
  39.  
  40. $dbh = NULL;
  41.  
  42. echo "\nProcessing...\n";
  43.  
  44. $handle = fopen($_SERVER["argv"][1], "r");
  45. if ($handle) {
  46.  
  47. $cntLines = 0;
  48. $newAreas = array();
  49.  
  50. while (($line = fgets($handle)) !== false) {
  51. $fields = array();
  52. $fields = explode(",", $line);
  53. $cntLines++;
  54.  
  55. if (sizeof($fields) !== 2) {
  56. echo "\nERROR: Missing field at line $cntLines.\n";
  57. $dbh = NULL;
  58. exit;
  59. }
  60.  
  61. $area = array();
  62.  
  63. // Field size checks
  64.  
  65. if (strlen($fields[1]) > 50 ) {
  66. echo "\nERROR: Max state_or_province field length (2) exceeded at line $cntLines.\n";
  67. $dbh = NULL;
  68. exit;
  69. } else { $area['state_or_province'] = $fields[0]; }
  70.  
  71. if (strlen($fields[0]) > 50 ) {
  72. echo "\nERROR: Max city field length (50) exceeded at line $cntLines.\n";
  73. $dbh = NULL;
  74. exit;
  75. } else { $area['city'] = $fields[1]; }
  76.  
  77. foreach ($index as $i) {
  78. if (trim($area['city'].$area['state_or_province']) === trim($i)) {
  79. echo "\nERROR: City ".$area['city']." already exists at line $cntLines.\n";
  80. $dbh = NULL;
  81. exit;
  82. }
  83. }
  84.  
  85. $newAreas[] = $area;
  86. }
  87.  
  88. fclose($handle);
  89. } else {
  90. echo "\nERROR: Couldn't open file.\n";
  91. $dbh = NULL;
  92. exit;
  93. }
  94.  
  95. $dbh = new PDO('mysql:host=localhost;dbname='.$dbName, $dbUser, $dbPass);
  96. $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  97. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  98. $area = array();
  99.  
  100. // Sanitize and insert into database
  101.  
  102. foreach ($newAreas as $area) {
  103. $area['city'] = trim($area['city']);
  104. $area['state_or_province'] = trim($area['state_or_province']);
  105. $area['city'] = preg_replace( "/\r|\n/", "", $area['city'] );
  106. $area['state_or_province'] = preg_replace( "/\r|\n/", "", $area['state_or_province'] );
  107. $stmt = $dbh->prepare('INSERT INTO isvr_cities (city, state_or_province) VALUES (:city, :state_or_province)');
  108. $stmt->execute(array('city' => $area['city'], 'state_or_province' => $area['state_or_province']));
  109. }
  110.  
  111. $dbh = NULL;
  112. echo "$cntLines processed\n";
  113. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement