Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从mysql获取数据,然后通过JSON显示_Php_Jquery_Json - Fatal编程技术网

Php 从mysql获取数据,然后通过JSON显示

Php 从mysql获取数据,然后通过JSON显示,php,jquery,json,Php,Jquery,Json,我想从mysql中获取数据,并通过json\u encode对其进行回显。然后我的JQUERY将通过$捕获JSON,并将结果附加到我的中 我试图弄明白为什么JQUERY捕获的数据没有打印在我的index.php上 这是我的index.php <html> <?php include_once('connect.php'); $sql = "SELECT * FROM data"; $query = mysql_query($sql);

我想从mysql中获取数据,并通过
json\u encode
对其进行回显。然后我的
JQUERY
将通过
$捕获
JSON
,并将结果附加到我的

我试图弄明白为什么JQUERY捕获的数据没有打印在我的index.php上

这是我的
index.php

<html>
<?php 

    include_once('connect.php');

    $sql    = "SELECT * FROM data";
    $query  = mysql_query($sql);
    $result = array(); 


    while($row = mysql_fetch_array($query))  
        array_push($result,                  
            array(                          
            'name' => $row[1],              
            'msg' => $row[2]));

    echo json_encode(array("key" => $result));      
?>

<body>

<ul>
    //insert fetch data here
</ul>



<script type="text/javascript" src="./js/jquery.js"></script>
<script type="text/javascript" src="./js/custom.js"></script>
</body>
</html>

    //在此处插入获取数据
这是我的JQUERY

function my_function() {
     $.getJSON("index.php", function(data) {
       $.each(data.key, function(){
       $("ul").append("<li>Name: "+this['name']+"</li>"
                      "<li>message: "+this['msg']+ "</li>");
       });
 });
}
函数我的函数(){
$.getJSON(“index.php”,函数(数据){
$.each(data.key,function(){
$(“ul”)。追加(“
  • 名称:“+this['Name']+”
  • ” “
  • 消息:“+this['msg']+”
  • ”; }); }); }
    首先:检查index.php输出是否显示有效的json。?是否存在无效的UTF8字符?在这种情况下,json_encode的输出为空

    second:我将使用普通ajax方法暗示json作为数据类型:

    jQuery.ajax({
           url: 'index.php',
           dataType: 'json'
    }).done(function(data) {
        for (i=1;i<data.length;i++) {
            datarow=data[i];
                $("ul").append("<li>Name: "+datarow.name+"</li>");
                $("ul").append("<li>message: "+datarow.msg+ "</li>");
            }
    
           });
    
    jQuery.ajax({
    url:'index.php',
    数据类型:“json”
    }).完成(功能(数据){
    
    对于(i=1;i您不需要通过JSON获取数据,因为您在同一页面上回显JSON。 你可以这样做:

    <ul id="jsonData" data-json-data="<?php echo json_encode($result;)?>"
    

    如果你想让一个php脚本返回json,那么json字符串必须是脚本唯一的东西
    echo
    d。你有一堆html,它们会导致无效的json。你在index.php中也有srctag吗?如果有,那就是你的问题,让它只返回json数组。如果没有,请更新帖子到你拥有的内容。你能扩展它吗关于“必须是脚本响应的唯一内容”的意思,首先要看到它返回JSON,请使用
    警报(数据);
    。最好使用单独的php文件,一个用于JSON结果,一个用于要显示
    ul
    {foo:“bar”}
    的页面。
    {foo:“bar”}不是有效的JSON。
    {code>{foo:“bar”}
    是有效的json。
    var myData = $('#jsonData').data('json-data');