PHP 5 中的 Json 编码问题

发布于 2024-12-09 08:26:22 字数 1456 浏览 1 评论 0原文

我正在尝试使用 php5 将 Json 与我的数据库一起使用,但遇到了奇怪的结果。 该数据库有四个字段 -“id”、“Title”、“Thread”、“date”,但 jason 结果如下所示。

[
   {
        "0": "1",
        "id": "1",
        "1": "Title 1",
        "Title": "Title 1",
        "2": "Thread 1",
        "Thread": "Thread 1",
        "3": "2011-10-19",
        "date": "2011-10-19"
    },
    {
        "0": "2",
        "id": "2",
        "1": "Title 2",
        "Title": "Title 2",
        "2": "Thread 2",
        "Thread": "Thread 2",
        "3": "2011-10-03",
        "date": "2011-10-03"
     }
]

可以看到结果中有重复的信息。他们来自哪里? 我将附上我编写的代码...... Jason & PHP高手请赐教:'(.. 预先感谢您..我会在等待您的帮助时再次尝试解决它....

    private static function queryAndFetch($tableName)
    {
        $query = "SELECT id, Title, Thread, date From $tableName";
        $result = mysqli_query(self::$link, $query);
        if(!($result))
        {
            echo "Error";
            exit;
        }


        // $posts = mysqli_fetch_assoc(self::$result); - Working
        self::$fetchedResult = array();
        while($row = mysqli_fetch_array($result))
        {
            self::$fetchedResult[] = $row;
        }
    }

    private static function encode()
    {
        //print_r(self::$fetchedResult);
        //if($format == 'json') {
        header('Content-type: application/json');
        echo json_encode(self::$fetchedResult);
        //}
        //echo "hi".json_last_error();
    }
}

I'm trying to use Json with my database using php5 but suffering from a weird result.
This database has four fields - 'id', 'Title', 'Thread', 'date' but the jason result looks like the following.

[
   {
        "0": "1",
        "id": "1",
        "1": "Title 1",
        "Title": "Title 1",
        "2": "Thread 1",
        "Thread": "Thread 1",
        "3": "2011-10-19",
        "date": "2011-10-19"
    },
    {
        "0": "2",
        "id": "2",
        "1": "Title 2",
        "Title": "Title 2",
        "2": "Thread 2",
        "Thread": "Thread 2",
        "3": "2011-10-03",
        "date": "2011-10-03"
     }
]

You can see there are duplicated information in the result. Where are they from??
I will attach the code I've written... Jason & PHP masters, please enlighten me :'(..
Thank you in advance.. I will try to solve it again as I wait for your help....

    private static function queryAndFetch($tableName)
    {
        $query = "SELECT id, Title, Thread, date From $tableName";
        $result = mysqli_query(self::$link, $query);
        if(!($result))
        {
            echo "Error";
            exit;
        }


        // $posts = mysqli_fetch_assoc(self::$result); - Working
        self::$fetchedResult = array();
        while($row = mysqli_fetch_array($result))
        {
            self::$fetchedResult[] = $row;
        }
    }

    private static function encode()
    {
        //print_r(self::$fetchedResult);
        //if($format == 'json') {
        header('Content-type: application/json');
        echo json_encode(self::$fetchedResult);
        //}
        //echo "hi".json_last_error();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筱武穆 2024-12-16 08:26:22

mysqli_fetch_array 返回具有关联键和枚举键的结果行。如果您只需要关联数组键,请使用 mysqli_fetch_assoc

mysqli_fetch_array returns the result rows with both associative and enumerated keys. If you only want the associative array keys, then use mysqli_fetch_assoc.

风启觞 2024-12-16 08:26:22

看起来您的 mysqli_fetch_array($result) 返回的是关联数组而不是索引数组。

尝试此更改:

while($row = mysqli_fetch_array($result)) {
    self::$fetchedResult[] = array(
        'id' => $row->id,
        'Title' => $row->Title,
        'Thread' => $row->Thread,
        'date' => $row->date
    );
}

如果这不起作用,请尝试使用 $row['id']、$row['Title']、$row['Thread'] 和 $row['date']。

或者,为了避免写出每个字段,请专门更改为 mysqli_fetch_assoc($result)。

我怀疑这就是你的问题?

谢谢,
我的流

It looks like your mysqli_fetch_array($result) is returning an associative array rather than indexed array.

Try this change:

while($row = mysqli_fetch_array($result)) {
    self::$fetchedResult[] = array(
        'id' => $row->id,
        'Title' => $row->Title,
        'Thread' => $row->Thread,
        'date' => $row->date
    );
}

If that doesn't work, try using $row['id'], $row['Title'], $row['Thread'] and $row['date'].

Alternatively, to avoid having to write out each field, change to mysqli_fetch_assoc($result) specifically.

I suspect this is the issue you've got?

Thanks,
MyStream

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文