dpy83214 2016-03-24 05:40
浏览 42
已采纳

用于MYSQL的PHP​​脚本在一个文件上工作,而不是在另一个文件上

I currently have an AngularJS application querying a database through a factory linked to a PHP script. The script below works exactly as planned and outputs the JSON used for Angular:

<?php
function getCompanyDetails($company_id)
    {
        // Connect to the database.
        require_once("config/connection.php");

        $query = "SELECT id, header, content FROM company_details WHERE company_id = ?;";

        $stmt = $conn->stmt_init();

        if($stmt->prepare($query)) {
            $stmt->bind_param("i", $company_id);
            $stmt->execute();

            $data = $stmt->get_result();

            while($row = $data->fetch_assoc()) {
                $result[] = $row;
            }

            header("Access-Control-Allow-Origin: *");
            header("Content-Type: application/json; charset=UTF-8");

            $stmt->close();
            mysqli_close($conn);
            echo json_encode($result);
        }
    }

getCompanyDetails(1);

?>

Now, I have other sections of the application which require the exact same action (Selecting results from the database) and I am using the following script (an almost identical copy to the one above):

<?php
function getCompanyProjects($company_id)
    {
        // Connect to the database.
        require_once("config/connection.php");

        $query = "SELECT title, description FROM company_projects WHERE company_id = ?;";

        $stmt = $conn->stmt_init();

        if($stmt->prepare($query)) {
            $stmt->bind_param("i", $company_id);
            $stmt->execute();
            $data = $stmt->get_result();

            while($row = $data->fetch_assoc()) {
                $result[] = $row;
            }

            header("Access-Control-Allow-Origin: *");
            header("Content-Type: application/json; charset=UTF-8");

            $stmt->close();
            mysqli_close($conn);
            echo json_encode($result);
        }
    }

getCompanyProjects(1);

?>

The problem is, the second script will not return any results. There are records in the database for the table and I believe I am closing the connection after each use. I have been stuck on this for a while now, does anybody have any idea of what the problem could be?

The query SELECT title, description FROM company_projects WHERE company_id = ?; works on phpMyAdmin.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • duangekui7451 2016-03-24 08:33
    关注

    After trying fetch_assoc(), fetch_array(), mysqli_fetch_assoc() and many others, resetting the server a few times, placing echos and var_dumps everywhere, I realized all the data was being retrieved up until the while section.

    When I actually iterated var_dump inside the while loop, I saw a little symbol for unrecognized character. There was an unescaped quote in one of the last results. After removing this little friend, fetch_assoc() started working well again. The problem was in the actual data, not on the code. Both scripts are working properly now with the correct data.

    Thanks for all the help, everyone! I had no idea what var_dump was before asking this question, a really useful function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起