以json格式导出数据

时间:2016-05-14 17:24:08

标签: php json

我正在尝试将数据从PhpMyAdmin导出为JSON格式
此代码适用于select lat,lng from googlemaps,但不适用于address列 我的代码是:

<?php 
$con=mysqli_connect("localhost","root","","googlemaps");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT lat,lng,`addr` FROM infos";
if ($result = mysqli_query($con, $sql))
{

$resultArray = array();
$tempArray = array();

// Loop through each row in the result set
while($row =mysqli_fetch_array($result))
{
    // Add each row into our results array
    $tempArray = $row;
    array_push($resultArray, $tempArray);
}


$fp = fopen('results01.json', 'w');
fwrite($fp, "{ \"tab\":  ");
fwrite($fp, json_encode($resultArray));
fwrite($fp, " }");
fclose($fp);
echo json_encode($resultArray);
//var_dump($resultArray);
?>

infos表的屏幕截图:

this is the data that i want to export

1 个答案:

答案 0 :(得分:1)

刚刚找到解决方案,它实际上是在选择查询之前添加它:

mysqli_set_charset($con, "utf8");
$sql = "SELECT lat,lng,`addr` FROM infos";
if ($result = mysqli_query($con, $sql))
{......}