<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Online PHP Script Execution</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "u948431288_root";
$password = "u948431288";
$dbname = "u948431288_stud";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
mysqli_query($conn,"SET NAMES utf8");
$sql = "SELECT id, name FROM KHtravel";
$result = $conn->query($sql);
$myarray[] = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$myarray[] = $row;
}
} else {
echo "0 results";
}
$fp = fopen('myarray.json', 'w');
fwrite($fp, json_encode($myarray,JSON_UNESCAPED_UNICODE));
fclose($fp);
mysqli_close($conn);
?>
</body>
</html>
- Sep 11 Fri 2015 18:53
[PHP] PHP使用MySQL查詢結果存為JSON檔案
文章標籤
全站熱搜
留言列表