pdo mysql fetchall,PDO循环通道和打印fetchAll

I'm having trouble getting my data from fetchAll to print selectively.

In normal mysql I do it this way:

$rs = mysql_query($sql);

while ($row = mysql_fetch_array($rs)){

$id = $row['id'];

$n = $row['n'];

$k = $row['k'];

}

In PDO, I'm having trouble. I bound the params, then I'm saving the fetched data into $rs like above, with the purpose of looping through it the same way..

$sth->execute();

$rs = $query->fetchAll();

Now comes the trouble part. What do I do PDO-wise to get something matching the while loop above?! I know I can use print_r() or dump_var, but that's not what I want. I need to do what I used to be able to do with regular mysql, like grabbing $id, $n, $k individually as needed. Is it possible?

Thanks in advance..

解决方案

It should be

while ($row = $query->fetch(PDO::FETCH_ASSOC)) {

$id = $row['id'];

$n = $row['n'];

$k = $row['k'];

}

If you insist on fetchAll, then

$results = $query->fetchAll(PDO::FETCH_ASSOC);

foreach($results as $row) {

$id = $row['id'];

$n = $row['n'];

$k = $row['k'];

}

PDO::FETCH_ASSOC fetches only column names and omits the numeric index.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值