-
Notifications
You must be signed in to change notification settings - Fork 0
/
facility_chemical.php
55 lines (50 loc) · 1.46 KB
/
facility_chemical.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
$mysqli = new mysqli('localhost', 'root', 'Tr!f0rce', 'pollution');
if ($mysqli->query('DELETE FROM facility_chemical')) {
echo "<p>facility_chemical table reset</p>";
} else {
echo $mysqli->error;
exit();
}
$facility_ids = array();
$counter = 0;
$fh = fopen("TRI_2009_US_v09.csv", "r") or die("can't open file");
while ($line = fgetcsv($fh)) {
if ($counter++ == 0) continue;
$facility = $line[1];
$facility_ids[$facility][] = array($line[24], $line[82]);
}
foreach ($facility_ids as $id=>$chemicals) { // where $id is the id from "facilities" table
//echo "<ul>$id";
foreach ($chemicals as $chemical) {
if ($result = $mysqli->query('SELECT id FROM facilities WHERE facility_id="'.$id.'"')) {
while ($row = $result->fetch_object()) {
$this_id = $row->id;
}
} else {
echo "there was something wrong with the SELECT query\n";
echo $mysqli->error;
exit();
}
if ($result = $mysqli->query('SELECT id FROM chemicals WHERE compound_id="'.$chemical[0].'"')) {
while ($row = $result->fetch_object()) {
$chem_id = $row->id;
}
} else {
echo $mysqli->error;
exit();
}
//echo "<li>".$chemical." - ".$chem_id."</li>";
if ($mysqli->query('INSERT INTO facility_chemical SET facilityid="'.$this_id.'", chemicalid="'.$chem_id.'", amount="'.$chemical[1].'"')) {
} else {
echo "<h1>The insert failed here</h1>";
echo $mysqli->error;
exit();
}
}
//echo "</ul>";
}
echo "done " . date("D, d M Y H:i:s T");
$mysqli->close();
exit();
?>