Created
November 11, 2015 18:42
-
-
Save anonymous/cb0c41b83ffbebb4362f to your computer and use it in GitHub Desktop.
Insert XML Data To MySQL using PDO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# important | |
@date_default_timezone_set("GMT"); | |
# create PDO object and intialise | |
$db = new PDO('mysql:host=localhost;dbname=bristol_transport;charset=utf8', 'root', ''); | |
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); | |
# create a simpleXML object with file as input | |
$xml = simplexml_load_file("transport_v1.xml"); | |
############### POPULATE ward TABLE ################################## | |
# set record counter | |
$rec = 0; | |
# get a array of wards (unique now) | |
$wards = $xml->xpath("//ward"); | |
# empty the ward table & reset AUTO_INCREMENT | |
$stmt = $db->query('TRUNCATE TABLE ward'); | |
# populate the ward table setting ward_id = $rec | |
foreach ($wards as $ward) { | |
# increment record counter | |
$rec++; | |
# insert data | |
$stmt = $db->prepare("INSERT INTO ward(`ward_id`, `name`, `lat`, `long`) VALUES(?, ?, ?, ?)"); | |
$stmt->execute(array($rec, $ward['name'], $ward['lat'], $ward['long'])); | |
} | |
############### POPULATE tmode TABLE ################################# | |
#re-set record counter | |
$rec = 0; | |
# get a array of transport modes | |
$modes = $xml->xpath("//ward[1]/mode"); | |
# empty the tmode table & reset AUTO_INCREMENT | |
$stmt = $db->query('TRUNCATE TABLE tmode'); | |
# populate the tmode table setting ward_id = $rec | |
foreach ($modes as $mode) { | |
# increment record counter | |
$rec++; | |
# insert data | |
$stmt = $db->prepare("INSERT INTO tmode(`tmode_id`, `mode`) VALUES(?, ?)"); | |
$stmt->execute(array($rec, $mode['type'])); | |
# build up look-up array for for tmode_id | |
$tmode[$rec] = $mode['type']; | |
} | |
############### POPULATE ward_tmode TABLE ############################ | |
#re-set record counter | |
$rec = 0; | |
# get the year value | |
$year = $xml->xpath("/transport/@year")[0]; | |
# empty the ward_tmode table & reset AUTO_INCREMENT | |
$stmt = $db->query('TRUNCATE TABLE ward_tmode'); | |
# populate the ward_tmode table setting id_ward = $rec & | |
# using the look-up array $tmode to find id_tmode | |
foreach ($wards as $ward) { | |
# increment record counter | |
$rec++; | |
# get a array of all mode entities in the current ward | |
$ward_modes = $xml->xpath("//ward[" . $rec . "]/mode"); | |
# populate table ward_tmode | |
foreach ($ward_modes as $wm) { | |
# look up key value for id_tmode | |
$key = array_search((string) $wm['type'], $tmode); | |
# insert data | |
$stmt = $db->prepare("INSERT INTO ward_tmode(`id_ward`, `id_tmode`, `year`, `percent`) VALUES(?, ?, ?, ?)"); | |
$stmt->execute(array($rec, $key, $year, $wm['percent'])); | |
} | |
} | |
# close db connection | |
$db=null; | |
# all done | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment