Remark : 웹화면에 XML 형식으로 나오기 하기
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 |
<?php // Database configuration $host = "localhost"; // Your database host $username = "username"; // Your database username $password = "password"; // Your database password $dbname = "database"; // Your database name $table = "table"; // Your database table // Create a new connection to the MySQL database $mysqli = new mysqli($host, $username, $password, $dbname); // Check for any connection errors if ($mysqli->connect_error) { die("Connection failed: " . $mysqli->connect_error); } // SQL query to select data from the table $query = "SELECT * FROM $table"; $result = $mysqli->query($query); // Check if there are any rows returned if ($result->num_rows > 0) { // Start XML file, echo parent node echo "<?xml version='1.0' encoding='UTF-8'?>"; echo "<root>"; // Iterate through the rows in the result set while($row = $result->fetch_assoc()) { echo "<item>"; echo "<id>" . $row["id"] . "</id>"; echo "<name>" . htmlspecialchars($row["name"]) . "</name>"; // Add more fields as needed echo "</item>"; } echo "</root>"; } else { echo "0 results"; } // Close the database connection $mysqli->close(); ?> |
1 2 3 4 |
header('Content-type: text/xml'); //xml type out echo $return; |
소스상에 echo 내용에 단지 xml 형식만 있어야 아래와 같이 출력.
Json 으로 출력
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 |
<?php // Database configuration $host = "localhost"; // Your database host $username = "username"; // Your database username $password = "password"; // Your database password $dbname = "database"; // Your database name $table = "table"; // Your database table // Create a connection to the MySQL database $mysqli = new mysqli($host, $username, $password, $dbname); // Check for any connection errors if ($mysqli->connect_error) { die("Connection failed: " . $mysqli->connect_error); } // SQL query to select data from the table $query = "SELECT * FROM $table"; $result = $mysqli->query($query); $data = array(); if ($result->num_rows > 0) { // Fetch each row in the result set while($row = $result->fetch_assoc()) { $data[] = $row; } } else { echo "0 results"; } |