【问题标题】:JSON returns [null,null] in my appJSON 在我的应用程序中返回 [null,null]
【发布时间】:2015-05-30 08:38:42
【问题描述】:

我想通过 json 将带有 PHPH 文件的数据库记录发送到我使用 IntelXDK 制作的应用程序。因为我不能在英特尔 XDK 中使用 PHP 代码,所以我需要使用 JSON。我想在屏幕上的“报价”表中显示两条记录“报价”和“作者”。有人帮我编写了这段代码,但它只返回 [null,null] 而不是我需要的两条记录。我尝试调试,但我是 PHP 新手,所以我无法让它工作。任何可以帮助或看到此代码中的错误?谢谢!

PS:是的,我现在已经有其他人就这个主题提出了多个问题。我已经阅读了所有内容,但没有一个能解决我的问题..

 <?php

    if(isset($_GET["get_rows"]))
    {
        //checks the format client wants
        if($_GET["get_rows"] == "json")
        {
            $link = mysqli_connect("localhost", "xxxxx", "xxxxx", "xxxx");

            /* check connection */
            if (mysqli_connect_errno()) {
                echo mysqli_connect_error();
                header("HTTP/1.0 500 Internal Server Error");
                exit();
            }

            $query = "SELECT quote, author FROM quotes WHERE id = " . date('d');

            $jsonData = array();

            if ($result = mysqli_query($link, $query)) {

                /* fetch associative array */
            $row = $result->fetch_assoc($result);

            // Create a new array and assign the column values to it
             // You can either turn an associative array or basic array
            $ret= array();

            $ret[] = $row['quote']; 
            $ret[] = $row['author']; 

                //encode to JSON format

                echo  json_encode($ret);

            }

            else {

                echo  json_encode($ret);
            }

            /* close connection */
            mysqli_close($link);
        }
        else
        {
            header("HTTP/1.0 404 Not Found");
        }
    }
    else
    {
        header("HTTP/1.0 404 Not Found");
    }


    ?>

【问题讨论】:

    标签: php mysql sql json database


    【解决方案1】:

    fetch_assoc() 函数调用存在错误 - 删除 $result 参数。如果您启用了错误报告,您应该会看到:

    警告:mysqli_result::fetch_assoc() 需要 0 个参数,给定 1 个

    只需将其更改为:

    $row = $result->fetch_assoc();
    

    在 javascript 中解析这个响应,只需这样做:

    var obj = JSON.parse(xmlhttp.responseText);
    document.getElementById("quote").innerHTML = obj[0];
    document.getElementById("author").innerHTML = obj[1];
    

    【讨论】:

    • 谢谢,这行得通!但是我现在得到 ["quote","author"],我实际上需要 quote 和 author 是两个单独的字符串,没有 "" 和 [].. 我的 json 输出这个: echo json_encode($ret);我的 html 文件上的脚本使用这个: document.getElementById("quote").innerHTML=xmlhttp.responseText;在
      中输出字符串你知道我怎么可以让json输出两个字符串,所以'quote'和'author'并让脚本输出下摆在两个不同的的?这将完全解决我的整个问题.. 谢谢!
    • 非常感谢!这终于完美了!这里的社区真棒!
    【解决方案2】:

    我认为你的问题在于 fetch_assoc() 尝试使用它:

        $row = mysqli_fetch_assoc($result);
    

    而不是

        $row = $result->fetch_assoc($result);
    

    你的例子对我有用

    【讨论】:

    • 谢谢,这行得通!但是我现在得到 ["quote","author"],我实际上需要 quote 和 author 是两个单独的字符串,没有 "" 和 [].. 我的 json 输出这个: echo json_encode($ret);我的 html 文件上的脚本使用这个: document.getElementById("quote").innerHTML=xmlhttp.responseText;在
      中输出字符串你知道我怎么可以让json输出两个字符串,所以'quote'和'author'并让脚本输出下摆在两个不同的的?那将完全解决我的整个问题.. 谢谢! ——
    【解决方案3】:

    改变这个

    $row = $result->fetch_assoc($result);
    

    $row = $result->fetch_assoc();
    

    只需将其更改为:

    $row = $result->fetch_assoc();
    

    更新:

    response =  JSON.parse(xmlhttp.responseText);
    

    您现在可以通过以下方式独立访问它们:

    reponse.quoteresponse.author

    【讨论】:

    • 谢谢,这行得通!但是我现在得到 ["quote","author"],我实际上需要 quote 和 author 是两个单独的字符串,没有 "" 和 [].. 我的 json 输出这个: echo json_encode($ret);我的 html 文件上的脚本使用这个: document.getElementById("quote").innerHTML=xmlhttp.responseText;在
      中输出字符串你知道我怎么可以让json输出两个字符串,所以'quote'和'author'并让脚本输出下摆在两个不同的的?这将完全解决我的整个问题.. 谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多