簡體   English   中英

嘗試將 sql 結果作為 json 數據類型回顯時出現語法錯誤

[英]Getting synxtax error when trying to echo sql results as json datatype

I'm trying to output the sql results of the statement in json datatype, but an error was prompt SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data , I have checked my sql statement and try to executed in sql 編輯器,它可以工作,但我不確定為什么當我嘗試在瀏覽器中回顯它時它不起作用。

So i have a big switch statement block, and this is a small part of it, so basically when i type http://localhost/w11/local-html/part_1/api/schedule it should output json datatype by taking in the arguments來自 url 之類的api/scheduleapiarg_1schedulearg_2

case 'api':
        {
            header("Content-Type: application/json");
            switch ($param2) {
                case 'schedule':
                {
                    switch ($param3) {
                        case '':
                        {
                            try {
                                $sqlQuery = "SELECT room, type, title, day, time FROM sessions INNER JOIN slots ON sessions.slotsID = slots.id";
                                $response = new JSONRecordSet();
                                $response = $response->getJSONRecordSet($sqlQuery, "");

                                echo $response;
                            } catch (PDOException $e) {
                                echo "Connection Failed:" . $e->getMessage();
                            }
                            break;
                        }
                        default:
                        {
                            //do something
                            break;
                        }

                    }
                    break;
                }

這是 getJSONRecordSet 的getJSONRecordSet

class JSONRecordSet extends RecordSet {

    function getJSONRecordSet($sql, $params = null) {
        $queryResult = $this->getRecordSet($sql, $params);

        $recordSet = $queryResult->fetchAll(PDO::FETCH_ASSOC);
        $nRecords = count($recordSet);
        if ($nRecords == 0) {
            $status =  200;
            $message = array("text" => "No records found");
            $result = '[]';
        }
        else {
            $status = 200;
            $message = array("text" => "");
            $result = $recordSet;
        }
        return json_encode(
            array(
                'status' => $status,
                'message' => $message,
                'data' => array(
                    "RowCount"=>$nRecords,
                    "Result"=>$result
                )
            ),
            JSON_PRETTY_PRINT
        );
    }
}

和父 class

abstract class RecordSet {
    protected $conn;
    protected $queryResult;

    function __construct() {
        $this->conn = pdoDB::getConnection();
    }

    function getRecordSet($sql, $params = null) {
        if (is_array($params)) {
            $this->queryResult = $this->conn->prepare($sql);
            $this->queryResult->execute($params);
        }
        else {
            $this->queryResult = $this->conn->query($sql);
        }
        return $this->queryResult;
    }
}

那么,我該如何解決這個錯誤呢?

這段代碼沒有任何問題。
由於SyntaxError: JSON.parse是一個 JS 錯誤,您應該檢查傳遞給它的數據。 檢查對您的請求的原始響應並進行一些調試。
這可能是您的數據庫連接有問題,或者您的 $param2 或 $param3 條件不滿足。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM