dpy83214 2016-03-23 21: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 00: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部