php json编码不输出任何东西

时间:2017-01-26 00:09:38

标签: php sql json

我有简单的PHP脚本,输出json,但它不起作用

<?php 

require 'connect.php';

$sql = "SELECT * FROM horizont";
$result = $con->query($sql);
$rows = array();
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
             $rows[] = $row;
    } 
} 
echo '<pre>';
var_dump($rows);
echo '</pre>';

echo json_encode($rows);
?>

我在var转储上获得结果,但不是json代码。

var dump结果:

http://prntscr.com/e0ef2c

connect.php

<?php
$con = mysqli_connect("localhost","root","","horizont");

// Check connection
 if (mysqli_connect_errno())
 {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }
 ?>

修正:

 $con->set_charset("utf8");

1 个答案:

答案 0 :(得分:1)

对于编码问题,请尝试:

$con->set_charset("utf8");