我有简单的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结果:
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");
答案 0 :(得分:1)
对于编码问题,请尝试:
$con->set_charset("utf8");