[英]PHP While Loop returning 1 result should be returning 15?
I wrote a function that should return all users in the database. 我编写了一个应返回数据库中所有用户的函数。 It's printing the array, but it's only printing one result. 它正在打印数组,但仅打印一个结果。 Why is it doing this? 为什么这样做呢? I surrounded it in a while loop and put a limit in my query... 我在一个while循环中将其包围,并在查询中设置了一个限制...
Code: 码:
function getAllUsers() {
global $PDO;
$stm = $PDO->prepare("SELECT * FROM `users` ORDER BY `bid` DESC LIMIT 15");
$stm->execute();
while($Array = $stm->fetch()) {
return print_r($Array);
}
}
Use fetchAll() : 使用fetchAll() :
$data = $stm->fetchAll()
foreach ($data as $item ) {
print_r($item);
}
use the foreach loop instead of the while and fetchAll() instead of the fetch(): 使用foreach循环代替while和fetchAll()代替fetch():
foreach ($stm->fetchAll() as $arr) {
print_r($arr);
}
function getAllUsers() {
global $PDO;
$stm = $PDO->prepare("SELECT * FROM `users` ORDER BY `bid` DESC LIMIT 15");
$stm->execute();
$var = array();
while($Array = $stm->fetch()) {
$var = $Array;
}
return $var;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.