source: src/db/csv_to_db.php@ 10925

Last change on this file since 10925 was 8989, checked in by dennisw, 14 years ago

csv_to_db.php - str_replace updated

File size: 1.6 KB
Line 
1<?php
2
3// edit with personal settings
4$location = 'localhost:3306';
5$username = 'root';
6$password = 'dsql';
7$database = 'project_heatmap';
8
9$filelist = array();
10$filelist = scandir("csv");
11sort($filelist);
12unset($filelist[0]);
13unset($filelist[1]);
14unset($filelist[2]);
15
16
17// connect to database
18mysql_connect($location, $username, $password)
19 or die("Error connecting to mysql: " . mysql_error());
20mysql_select_db($database)
21 or die("Error connecting to database: " . mysql_error());
22
23
24
25foreach($filelist as $file) {
26
27$table = substr($file, 0, -4);
28
29 // create table
30 mysql_query("
31 CREATE TABLE IF NOT EXISTS ".$table." (
32 latitude double(9,7) NOT NULL,
33 longitude double(9,7) NOT NULL,
34 ssid varchar(45) NOT NULL,
35 mac varchar(45) NOT NULL,
36 encryption tinyint(4) NOT NULL,
37 ID int(11) NOT NULL AUTO_INCREMENT,
38 PRIMARY KEY (ID)
39 )
40 ");
41
42
43 // .csv file to open
44 if (($handle = fopen("csv/".$file, "r")) !== FALSE) {
45 while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
46 $lat=str_replace("N ", "", $data[0]);
47 $lon=str_replace("E ", "", $data[1]);
48 $ssid=str_replace("'", "", $data[2]);
49 $enc = substr($data[8], -2, 1);
50 echo "\"".$lat.", ".$lon."\", \"".$data[2]."\", \"".$data[4]."\", \"".$enc."\"<br />\n";
51 $query=mysql_query("INSERT INTO ".$table." (latitude, longitude, ssid, mac, encryption) VALUES ('$lat','$lon','$ssid','$data[4]','$enc')");
52 if (!$query) {
53 die('Invalid query: ' . mysql_error());
54 }
55 }
56 fclose($handle);
57 rename("csv/".$file, "csv/backup/".$file);
58 }
59
60}
61
62?>
Note: See TracBrowser for help on using the repository browser.