Home > Backend Development > PHP Tutorial > 为什么这个arr不是个数组?

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:16:55
Original
1325 people have browsed it

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

<code><?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'];
    
}
?></code>
Copy after login
Copy after login

回复内容:

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

<code><?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'];
    
}
?></code>
Copy after login
Copy after login

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

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

你的方法有问题

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

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

<code>$arr[]=$v['username'];</code>
Copy after login

}

Related labels:
php
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template