[英]Sql query returns only first row
I'm try to display a result of a select query, but i only get duplicate first row not all the rows. 我试图显示选择查询的结果,但我只会得到重复的第一行,而不是所有行。 here is my code :
这是我的代码:
$query = "SELECT Email from client";
$result = $db->query($query)->fetch();
foreach($result as $email){
echo $email["Email"]."\n";
}
Connexion to database works fine. 与数据库的连接正常。
You need to use fetchALL()
as fetch()
only returns one row according to the docs: 您需要使用
fetchALL()
因为fetch()
仅根据文档返回一行 :
Fetches a row from a result set associated with a PDOStatement object.
从与PDOStatement对象关联的结果集中获取一行 。
$query = "SELECT Email from client";
$result = $db->query($query)->fetchALL();
foreach($result as $email){
echo $email["Email"]."\n";
}
The fetch()
function only fetches one record. fetch()
函数仅读取一条记录。
Modify your code like this: 像这样修改您的代码:
$query = "SELECT Email from client";
$res = $db->query($query);
while($result = $res->fetch()){
echo $result["Email"]."\n";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.