Home  >  Article  >  Backend Development  >  我这个只想echo一条数据怎么办?

我这个只想echo一条数据怎么办?

WBOY
WBOYOriginal
2016-06-06 20:08:13947browse

year字段下有很多数据 这样可以echo出来很多数据 这些数据都是一样的 所以我只想要echo出来一条数据 应该怎么办呢?

<code>$res=$stmt->fetchall(PDO::FETCH_ASSOC);
foreach($res as $v){
    echo $v['year'];
}</code>

回复内容:

year字段下有很多数据 这样可以echo出来很多数据 这些数据都是一样的 所以我只想要echo出来一条数据 应该怎么办呢?

<code>$res=$stmt->fetchall(PDO::FETCH_ASSOC);
foreach($res as $v){
    echo $v['year'];
}</code>

第一个,如果你是想要,查询所有,输出一个year的话,那么 $v = current($res); echo $v['year'];
第二个,如果你是想要,查出一条输出的话,那么查询语句加上 limit 1
第三个,如果你是查询出来很多个year,里面又有重复的话. var_dump(array_unique(array_column($res,'year'));)

1.只取一条的话,不要用fetchAll,用fetch
$res=$stmt->fetch(PDO::FETCH_ASSOC);
2.原SQL语句中加入limit 1

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn