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

WBOY
Release: 2016-06-06 20:08:13
Original
951 people have browsed it

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

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

回复内容:

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

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

第一个,如果你是想要,查询所有,输出一个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

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!