PDO中的非法字符串偏移对于每个循环 [英] Illegal String Offset within PDO For Each loop
本文介绍了PDO中的非法字符串偏移对于每个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
PHP查询:
<?php
$ query = $ db->准备('选择*从地点在哪里user_id =:id限制0,5');
$ query-> bindParam(:id,$ id);
$ result = $ query-> execute();
$ rows = $ query-> fetch();
foreach($ rows为$ row){
echo $ row [timestamp]; < br />;
}
?>
应该打印的两行(timestamp):
实际打印的内容:
1188((22
)控制台内的错误:
PHP警告:/ Sites / pages / user_account中的非法字符串偏移量'timestamp'第73行中的.php第73行是echo $ row ... forloop。
任何帮助将不胜感激。
您正在使用
fetch
,它检索单个行,而不是 fetchAll
: $ rows = $ query-> fetchAll();
PHP Query:
<?php
$query = $db->prepare('SELECT * FROM Locations WHERE user_id = :id LIMIT 0, 5');
$query->bindParam(":id",$id);
$result = $query->execute();
$rows = $query->fetch();
foreach ($rows as $row) {
echo $row["timestamp"]; "<br />";
}
?>
The two rows that should be printed (timestamp):
What actually prints: 1188((22
The error within the console: PHP Warning: Illegal string offset 'timestamp' in /Sites/pages/user_account.php on line 73 - Line 73 being the echo $row... inside the forloop.
Any help would be greatly appreciated.
解决方案
You are using fetch
, which retrieves a single row, instead of fetchAll
:
$rows = $query->fetchAll();
这篇关于PDO中的非法字符串偏移对于每个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文