Skip to content
Permalink
24d0e69395
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
112 lines (67 sloc) 2.74 KB
<?php
$dbServername = "68.183.39.178";
$dbUsername="test5";
$dbPassword="test6";
$dbName="DB";
// Database information
$conn =mysqli_connect($dbServername,$dbUsername,$dbPassword,$dbName);
//Storing the connection to the databse in a variable conn
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = " SELECT ID ,Name, Address, History FROM Buildings";
// sql query to select desired information from database
$buildingsArray= array();
//create an array
if ($result =$conn->query($query)){
//run query to get result set of data from database
while ($row = $result->fetch_assoc()){
//loop through result set and fetch rows
array_push($buildingsArray,$row);
// rows are now pushed on to a array
}
if(count($buildingsArray)){
build_document($buildingsArray);
// if their are elements in the array then build xml documnet
}
$result->free();
}
$conn->close();
//close the connection to themysql database
function build_document($buildingsArray){
$filePath = 'building.xml';
// create xml
$dom = new DOMDocument('1.0', 'utf-8');
$root = $dom->createElement('buildings');
// root of document called buildings
for($count=0; $count<count($buildingsArray); $count++){
// loop through until result set of buildings
$buildingID = ($buildingsArray[$count]['ID']);
$buildingName = htmlspecialchars($buildingsArray[$count]['Name']);
$buildingAddress = ($buildingsArray[$count]['Address']);
$buildingHistory = htmlspecialchars($buildingsArray[$count]['History']);
// assign the results to my own variables
$building = $dom->createElement('building');
$building->setAttribute('ID', $buildingID);
// create header building in xml file
// add building id to xml file
$name = $dom->createElement('Name', $buildingName);
$building->appendChild($name);
// create header Name in xml file
// add building name to xml file
$address = $dom->createElement('Address', $buildingAddress);
$building->appendChild($address);
//create header Address in xml file
//add address to xml file
$history = $dom->createElement('History', $buildingHistory);
$building->appendChild($history);
// create header History in xml document
// add history to xml to file
$root->appendChild($building);
// add header to the bittom callled buildings
}
$dom->appendChild($root);
$dom->save($filePath);
}
?>