使用fetch_assoc从数据集中获取特定行?

时间:2013-06-26 16:14:13

标签: php mysql loops while-loop dataset

如果我使用以下查询,并假设它返回以下数据集

SELECT p.`id`, 
    s.`name`, 
    d.`deployStatus`,
    d.`deployedDate`
FROM `patches` AS p
LEFT JOIN `deployed` AS d ON d.`PatchID` = p.`id`
LEFT JOIN `servers` AS s ON s.`id` = d.`serverID`
WHERE p.`id` = 2

| id | name | deployStatus | deployedDate |
|____|______|______________|______________|
| 2  | test |      1       |  06/14/2013  |
| 2  | prod |     null     |  06/14/2013  |

while($row = $results->fetch_assoc()){命令是否有办法只返回“test”行,而不更改sql查询?

1 个答案:

答案 0 :(得分:1)

使用if条件并检查它是否是您想要的行。

while($row = $results->fetch_assoc()){
     if($row['name'] == "test") {
         // echo here your result
     }
}