-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.php
55 lines (45 loc) · 1.26 KB
/
export.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
include('db.php');
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM addressbook";
$result = mysqli_query($conn, $sql);
//EXPORT XML DATA
if($_GET['data'] == 'xml'){
$xml = "<root_contact>";
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$xml .= '<contact name="'.$row["name"].'" firstname="'.$row["fname"].'" email="'.$row["email"].'" street="'.$row["street"].'" city="'.$row["city"].'" zipcode="'.$row["zipcode"].'" />';
}
}
$xml .= "</root_contact>";
$sxe = new SimpleXMLElement($xml);
$dom = new DOMDocument('1,0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($sxe->asXML());
echo $dom->saveXML();
$dom->save('xmldata.xml');
header('location: xmldata.xml');
exit;
}
//EXPORT JSON DATA
if($_GET['data'] == 'json'){
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
$peoplearray[] = $row;
}
echo json_encode($peoplearray);
$fp = fopen('jsondata.json', 'w');
fwrite($fp, json_encode($peoplearray));
fclose($fp);
header('location: jsondata.json');
exit;
}
$conn->close();