PHP无法将数组从选定表填充到json_encode并在javascript上接收它 [英] PHP can't populate array from selected table to json_encode and receive it on javascript
问题描述
我正在基于JqueryMobile构建一个混合应用程序.现在,我正在尝试获取一个简单的数组,其中包含从mysql表中选择的类别.我看到了与此相关的许多问题,并厌倦了上千种解决方案,但无济于事.
I'm build an hybrid app based on JqueryMobile. And now I'm trying to get a simple array with the categorys that I select from my mysql table. I saw many questions related to this and tired a thousand solutions but nothing worked.
这就是我所拥有的:
PHP代码:
$sql=mysql_query("SELECT Tipo FROM tfc_db.Category ");
$id=array();
while($row = mysql_fetch_assoc($sql)) {
$id[] = $row;
//$id[] = $row['Tipo']; doesn't work either
}
echo json_encode($id);
mysql_close($conn);
我得到空白结果.
我最接近的是这个:
$id=array();
$i=0;
while ($row = mysql_fetch_assoc($sql)) {
$id[] = array("data$i" => $row['Tipo']);
echo json_encode($id[$i]);
$i++;
}
结果:
{"data0":"Restaurantes"}{"data2":"Shopping"}{"data3":"Eventos"}{"data4":"Hoteis"}{"data5":"Oficinas"}{"data6":"Combustiveis"}
但这显然是错误的,因为它具有多个json_encode调用.
but this is obviously wrong because it has multiple json_encode invocations.
我正在尝试通过Jquery的.$ get
Im trying to get it with Jquery's .$get
<script type="text/javascript">
var inicio=function(argument){
$("#submit").click(function(e){
e.preventDefault();
$.get('http://myurl/get_category.php',{
},function(answer){
alert(answer.length+" "+answer); //trying to know whats happening with the data.
for (var i = 0; i < answer.length; i++) { //my final objective is to fill a listview.
$('#list').append("<li> <h3>"+answer.tipo+"</h3></a></li>");
}
$('#list').listview('refresh');
},"json"
);
});
}
$(document).on('ready',inicio);
</script>
正如我所说,我搜索了许多问题和解决方案,但总是得到空值.我在PhP方面没有太多的经验,这是我在JqueryMobile上学习并积累经验,所以..这里有些错误,我看不到..
As I said I searched into many questions and solutions but always got null values. I don't have too much experience with PhP and this is me studying and making experiences with JqueryMobile so.. something's wrong here that I can't see..
谢谢.
推荐答案
Found the solution here on this post Everything on the DB must be set to UTF-8 or utf8mb4 and now it's finally working. Apreciated everyone's help :)
这篇关于PHP无法将数组从选定表填充到json_encode并在javascript上接收它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!