0

好的,所以我有一个响应 GET HTTP 请求的 php;这个 php 脚本调用数据库,并检索一组行。

$results = $db->pdoquery("SELECT * FROM posts JOIN users ON posts.user_id=users.id ORDER BY posts.cre DESC LIMIT 4");
    $json = json_encode($results);
    echo $json;

现在,当我使用backbonejs '.fetch' 命令时,它只返回第一条记录,而不是四个。

如果我更改 php 脚本,我会得到所有四个,但我必须失去我的 JOIN

$results = $db->pdoquery("SELECT * FROM posts ORDER BY posts.cre DESC LIMIT 4");
    $json = json_encode($results);
    echo $json;

谁能解释我做错了什么?

更新:php 页面打印的 JSON

[{"id":"7","post_name":"title","pretty_url":"link1","preface":"a preface 7","body_text":"a body","feat":"0","img":"\/img\/logo.png","cre":"2012-07-22","modi":"2012-07-18","user_id":"2"},{"id":"6","post_name":"title","pretty_url":"link","preface":"a preface 6","body_text":"a body1","feat":"0","img":"\/img\/logo.png","cre":"2012-07-21","modi":"2012-07-18","user_id":"2"},{"id":"5","post_name":"TestTitle 1","pretty_url":"testlink4","preface":"a preface 5","body_text":"a body 2","feat":"0","img":"\/img\/logo.png","cre":"2012-07-20","modi":"2012-07-16","user_id":"2"},{"id":"4","post_name":"TestTitle 1","pretty_url":"testlink3","preface":"a preface 4","body_text":"a body 3","feat":"0","img":"\/img\/logo.png","cre":"2012-07-19","modi":"2012-07-16","user_id":"2"}]
4

1 回答 1

0

我发现了问题。

Posts 和 Users 具有名称相同的列,主要是 'id',并且由于这四个 post 记录具有相同的 user_id,因此将 'id' 列设置为相同的值。

Backbone 必须有内置的东西来跳过它认为是相同的记录或类似的东西。只需更详细地描述我需要的内容即可解决此问题。

不管修复它的代码都在下面

        $results = $db->pdoquery("SELECT 
        posts.post_name, posts.pretty_url, posts.preface, posts.img, posts.cre, posts.modi, users.username
        FROM posts 
            JOIN users 
                ON posts.user_id=users.id 
                ORDER BY posts.cre 
                DESC LIMIT 4");
    $json = json_encode($results);
    echo $json;

感谢所有帮助过的人。

于 2012-08-31T22:44:27.817 回答