Advertisement
Guest User

AdiKhajuria-CS50-Pset8-mashup-import

a guest
Nov 16th, 2016
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.33 KB | None | 0 0
  1. #!/usr/bin/env php
  2. <?php
  3.  
  4.     // TODO
  5.     /*
  6.     *
  7.     * check if the file exists and is readable
  8.     * fopen on the file name
  9.     * once the file is open
  10.     * use fgetcsv to read the file's lines in a loop into an array
  11.     * use CS50::query to read each line into the database using INSERT IGNORE
  12.     * once we are done fclose to close the file
  13.     */
  14.     require('../includes/config.php');
  15.    
  16.     if($argc == 2)
  17.     {
  18.         $fileName = $argv[1];
  19.         if( file_exists($argv[1]) && is_readable($argv[1]) )
  20.         {
  21.             $addressFile = fopen($fileName, "r");
  22.            
  23.             $placeLine = [];
  24.            
  25.             while( $placeLine = fgetcsv($addressFile, 200 , "\t") !== false )
  26.             {
  27.                 $queryString = 'INSERT IGNORE INTO places (country_code, postal_code, place_name, admin_name1, admin_code1, admin_name2, admin_code2, admin_name3, admin_code3, latitude,longitude, accuracy)';
  28.                 $queryString .= ' VALUES(?,?,?,?,?,?,?,?,?,?,?,?)';
  29.            
  30.                 CS50::query("${queryString}", $placeLine[0], $placeLine[1], $placeLine[2],$placeLine[3],$placeLine[4],$placeLine[5],$placeLine[6],$placeLine[7], $placeLine[8], $placeLine[9],$placeLine[10],$placeLine[11]);
  31.                
  32.             }
  33.             fclose($addressFile);
  34.         }
  35.     }
  36.    
  37. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement