douzi6992 2014-11-22 10:23
浏览 57
已采纳

PHP无法将选定表中的数组填充到json_encode并在javascript上接收它

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.

Here's what I have:

PHP Code:

$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);

with this I get blank result.

The closest I got was with this:

$id=array();
$i=0;
while ($row = mysql_fetch_assoc($sql)) { 
    $id[] = array("data$i" => $row['Tipo']);
    echo json_encode($id[$i]);
    $i++;
}

with the result:

{"data0":"Restaurantes"}{"data2":"Shopping"}{"data3":"Eventos"}{"data4":"Hoteis"}{"data5":"Oficinas"}{"data6":"Combustiveis"}

but this is obviously wrong because it has multiple json_encode invocations.

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>

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..

Thank you.

  • 写回答

4条回答 默认 最新

  • drhgzx4727 2014-11-23 23:33
    关注

    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 :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿