-
Notifications
You must be signed in to change notification settings - Fork 7
/
SQLtoXML_PHP.php
48 lines (36 loc) · 1.07 KB
/
SQLtoXML_PHP.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
<?php
$cd = dirname(__FILE__);
// INITIALIZE XML DOCUMENT
// create a dom document with encoding utf8
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
$root = $dom->createElement("CLData");
$root = $dom->appendChild($root);
// CONNECT TO DATABASE
try {
$dbh = new PDO("sqlite:".$cd."/CLData.db");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM cldata";
$sth = $dbh->query($sql);
$sth->setFetchMode(PDO::FETCH_ASSOC);
$data = $sth->fetchAll();
}
catch(PDOException $e) {
echo $e->getMessage();
exit;
}
// BUILD XML BY DATA ROWS
foreach($data as $d) {
$mcNode = $dom->createElement('missedConnection');
$mcNode = $root->appendChild($mcNode);
foreach($d as $k=>$v) {
$childNode = $dom->createElement($k, htmlspecialchars($v));
$mcNode->appendChild($childNode);
}
}
file_put_contents($cd. "/CLData_PHP.xml", $dom->saveXML());
// CLOSE DB CONNECTION
$dbh = null;
echo "Successfully migrated SQL data to XML!\n";
?>