Guest User

Untitled

a guest
Jun 18th, 2018
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. <?php
  2. $db_url = 'pgsql://user:password@host/dbname';
  3. $conn = db_connect($db_url);
  4. $file = fopen("fileToImport.csv", "r");
  5. echo "Importing file....\n";
  6. echo "Let me a second...\n";
  7. while ($row = fgetcsv($file, 10000, ",") )
  8. {
  9. $luminario = $row[3];
  10. $tipo = $row[2];
  11. $numero = $row[1];
  12. $calle = $row[0];
  13. $result = pg_query($conn,"INSERT INTO googlemaps_data (tipo_luminaria,tipo_poste,poblado,calle,numero_poste) values ('{$luminario}', '{$tipo}','Mexico', '{$calle}', '{$numero}')");
  14. if (!$result) {
  15. echo "An error occured.\n";
  16. exit;
  17. }
  18. }
  19. fclose($file);
  20. echo "Importing Done...\n";
  21. function db_connect($url)
  22. {
  23. $url = parse_url($url);
  24. $conn_string = '';
  25. if (isset($url['user'])) {
  26. $conn_string .= ' user=' . urldecode($url['user']);
  27. }
  28. if (isset($url['pass'])) {
  29. $conn_string .= ' password=' . urldecode($url['pass']);
  30. }
  31. if (isset($url['host'])) {
  32. $conn_string .= ' host=' . urldecode($url['host']);
  33. }
  34. if (isset($url['path'])) {
  35. $conn_string .= ' dbname=' . substr(urldecode($url['path']), 1);
  36. }
  37. if (isset($url['port'])) {
  38. $conn_string .= ' port=' . urldecode($url['port']);
  39. }
  40. $connection = @pg_connect($conn_string);
  41. if (!$connection)
  42. {
  43. exit;
  44. }
  45. return $connection;
  46. }
  47. ?>
Add Comment
Please, Sign In to add comment