0

このphpコードでjson構造を作成します:

<?php
    include "../base.php";

            $STH = $DBH->prepare("SELECT * FROM customers");
            $STH->execute();

            $result = $STH->fetchall(PDO::FETCH_ASSOC);
            $rows = $STH->rowCount();

            $jsontext = "{";
            $jsontext .= "total: ".$rows.",";
            $jsontext .= "page: 0,";
            $jsontext .= "records: [";

            foreach($result as $key => $inner_arr) {
                $jsontext .= "{";
                foreach($inner_arr as $field_name => $field_value) {
                    $jsontext .= "{$field_name}: {$field_value}, ";
                }
                $jsontext .= "},";
            }

            $jsontext .= "]";
            $jsontext .= "}";
    echo json_encode($jsontext);
?>

主な問題はこの行です$jsontext .= "{$field_name}: {$field_value}, ";

スクリプト全体を「echo」で印刷すると機能します。しかし、$jsontext .=それは機能せず、「null」しか受け取りません。

4

4 に答える 4

0

組み込み関数 json_encode を使用してみてください

json_encode

于 2013-09-01T11:27:49.383 に答える