首页 > 为什么这个arr不是个数组?

为什么这个arr不是个数组?

我就是想把查询结果放到一个数组里面 改怎么写呢 我的哪里不对呢?

<?php
$pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
$stmt=$pdo->prepare("select * from user");
$stmt->execute();
$res=$stmt->fetchall(PDO::FETCH_ASSOC);
$arr=array();
foreach($res as $v){
    $arr=$v['username'];
    
}
?>

$arr=$v['username'];这样$arr一直在被覆盖!你是想表达这个意思吧: $arr[]=$v['username']


你的方法有问题

foreach($res as $v){
    $arr=$v['username'];
}

建议修改成
foreach($res as $v){

$arr[]=$v['username'];

}


你这样写的 话 所有的 结果一直只有一个 就是 $arr[0],

【热门文章】
【热门文章】