在PHP中使用重音符号处理JSON对象
我试图从MySQL结果创建一个json对象,但没有得到我需要的结果。我不知道问题出在哪里。我在本地主机上执行,没有任何东西(白色窗口) 我想要这样的东西:在PHP中使用重音符号处理JSON对象,php,mysql,arrays,json,multidimensional-array,Php,Mysql,Arrays,Json,Multidimensional Array,我试图从MySQL结果创建一个json对象,但没有得到我需要的结果。我不知道问题出在哪里。我在本地主机上执行,没有任何东西(白色窗口) 我想要这样的东西: { arraytwo:{ 0:{pid:"..",name:"..",year:"..",subject:".."}, 1:{pid:"..",name:"..",year:"",subject:".."}, 2:{pid:"..",name:"..",year:
{
arraytwo:{ 0:{pid:"..",name:"..",year:"..",subject:".."},
1:{pid:"..",name:"..",year:"",subject:".."},
2:{pid:"..",name:"..",year:"..",subject:".."},
...},
success:1
}
//-------------我的代码在这行下面--------------------------
$response = array(); //array one (multi.)
$con = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_DATABASE);
$result = mysqli_query($con, "SELECT * FROM classroom") or die(mysqli_error());
if (mysqli_num_rows($result) > 0) {
$response["arraytwo"] = array(); //array two
while ($row = mysqli_fetch_array($result,MYSQLI_BOTH)) {
$arraythree = array(); //array three
$arraythree["pid"] = $row["pid"];
$arraythree["name"] = $row["name"];
$arraythree["year"] = $row["year"];
$arraythree["subject"] = $row["subject"];
array_push($response["arraytwo"], $arraythree);
}
$response["success"] = 1;
echo json_encode($response);
}
这是我的真实代码:
<?php
$response = array();
$con = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_DATABASE);
$result = mysqli_query($con, "SELECT *FROM products") or die(mysqli_error());
if (mysqli_num_rows($result) > 0) {
$response["noticias"] = array();
while ($row = mysqli_fetch_array($result,MYSQLI_BOTH)) {
$noticia = array();
$noticia["pid"] = $row["pid"];
$noticia["nombre"] = $row["nombre"];
$noticia["curso"] = $row["curso"];
$noticia["asignatura"] = $row["asignatura"];
$noticia["accion"] = $row["accion"];
$noticia["descripcion"] = $row["descripcion"];
$noticia["created_at"] = $row["created_at"];
$noticia["updated_at"] = $row["updated_at"];
array_push($response["noticias"], $noticia);
}
echo json_encode($response);
$response["success"] = 1;
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "No notices found";
echo json_encode($response);
}
?>
您需要使用utf8_编码转换字符。将代码更改为:
$noticia["pid"] = $row["pid"];
$noticia["nombre"] = utf8_encode($row["nombre"]);
$noticia["curso"] = $row["curso"];
$noticia["asignatura"] = utf8_encode($row["asignatura"]);
$noticia["accion"] = $row["accion"];
$noticia["descripcion"] = utf8_encode($row["descripcion"]);
$noticia["created_at"] = $row["created_at"];
$noticia["updated_at"] = $row["updated_at"];
在phpmyadmin中执行SELECT*FROM checkool并告诉您看到了什么我看到了表中的3行。我不知道我的问题是什么我编辑我的答案,看一看我把我的真实代码,我想你可以看到所有信息的问题:/。我尝试了你的答案,我发现了同样的问题(没有)。我要输掉这场与php的战斗。非常感谢。你解决了我的问题,现在我真的很感谢你。我也理解我的问题。小心点,阿德里安。@SalvaS,不客气,你什么时候可以投票表决;-)
$noticia["pid"] = $row["pid"];
$noticia["nombre"] = utf8_encode($row["nombre"]);
$noticia["curso"] = $row["curso"];
$noticia["asignatura"] = utf8_encode($row["asignatura"]);
$noticia["accion"] = $row["accion"];
$noticia["descripcion"] = utf8_encode($row["descripcion"]);
$noticia["created_at"] = $row["created_at"];
$noticia["updated_at"] = $row["updated_at"];