我尝试显示选择查询的结果,但我只得到重复的第一行,而不是所有行。这是我的代码:
$query = "SELECT Email from client";
$result = $db->query($query)->fetch();
foreach($result as $email){
echo $email["Email"]."\n";
}
与数据库的连接工作正常。
最佳答案
您只需要将fetchALL()
用作fetch()
returns one row根据文档:
Fetches a row from a result set associated with a PDOStatement object.
$query = "SELECT Email from client";
$result = $db->query($query)->fetchALL();
foreach($result as $email){
echo $email["Email"]."\n";
}
关于php - SQL查询只返回第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36502638/