为什么PDO fetchAll()返回两次相同的结果?

时间:2019-07-02 23:55:24

标签: php pdo

我正在使用PDO在PHP中构建一个简单的搜索引擎。一切正常,但是fetchAll(PDO :: FETCH_ASSOC)两次返回相同的结果。

这是我的PHP代码: (已更新以显示 id 列...)

header('Content-Type: application/json');

$q = db()->prepare("SELECT id, title, description, type, mediaId FROM projects WHERE title LIKE ?");
$q->execute(array('%'.$search.'%'));
$d = $q->fetchAll(PDO::FETCH_ASSOC);

echo json_encode($d);

这是我从PDO获得的结果

[
{
"id" : "1",
"title": "PhonArt",
"description": "Games",
"type": "",
"mediaId": "linkA"
},
{
"id" : "1",
"title": "PhonArt",
"description": "Games",
"type": "",
"mediaId": "linkA"
},
{
"id" : "2",
"title": "BioState",
"description": "Biology",
"type": "",
"mediaId": "linkB"
},
{
"id" : "3",
"title": "SnaPhones Inc.",
"description": "A social network",
"type": "",
"mediaId": "linkC"
},
{
"id" : "2",
"title": "BioState",
"description": "Biology",
"type": "",
"mediaId": "linkB"
},
{
"id" : "3",
"title": "SnaPhones Inc.",
"description": "A social network",
"type": "",
"mediaId": "linkC"
}
]

0 个答案:

没有答案