使用PHP将数据从MySQL转换为JSON [英] Convert Data from MySQL to JSON using PHP
问题描述
我正在尝试使用PHP脚本将数据从数据库转换为JSON. 在我的本地环境(MAMP)上,一切都很好. 当我尝试将所有内容放到服务器上时,我的JSON文件为空:(
I am trying to convert data from my DB to JSON with a PHP script. Everything works great on my local environment (MAMP). As soon as I am trying to put things together on my server, my JSON File is empty :(
到目前为止我所拥有的
我的php脚本
<?php
$connection = mysqli_connect("localhost","root","root","angulardb") or die("Error " . mysqli_error($connection));
$sql = "select * from postings";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
$fp = fopen('menue.json', 'w');
fwrite($fp, json_encode($emparray));
fclose($fp);
echo json_encode($emparray);
?>
通过将此脚本上传到我的服务器(并更新数据),我收到了一个空的JSON文件,没有任何响应.
By uploading this script to my server (& update the data), I receive an empty JSON file with no response.
任何想法都可能出问题吗??? 服务器端是否需要其他配置?
Any Idea what can be wrong??? Is there additional configuration necessary on the server side?
预先感谢
推荐答案
好, 知道了.
缺少chsrset = utf8导致了我的问题!
the missing chsrset=utf8 caused my problem!
$ pdo = new PDO("mysql:dbname = db239256x2330361; host = mysql.webhosting38.1blu.de; charset = utf8 ","s239256_2330361","20dj93lpav + dd);
$pdo=new PDO("mysql:dbname=db239256x2330361;host=mysql.webhosting38.1blu.de;charset=utf8","s239256_2330361","20dj93lpav+dd");
这篇关于使用PHP将数据从MySQL转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!