PHP PDO fetch 返回一个数组?
$GetUid = $dbConnect->prepare("SELECT UID FROM users WHERE username = :username");
$GetUid->execute($RegisterData3);
$UserID = $GetUid->fetch();
为什么它返回数组而不是字符串?
var_dump('$UserID') 表示
array
'UID' => string '45' (length=2)
0 => string '45' (length=2)
应该
array
'UID' => string '45' (length=2)
更新* 那 0 呢?它从哪里来?感谢您的回复。
$GetUid = $dbConnect->prepare("SELECT UID FROM users WHERE username = :username");
$GetUid->execute($RegisterData3);
$UserID = $GetUid->fetch();
why does it return array not a string ?
var_dump('$UserID') says
array
'UID' => string '45' (length=2)
0 => string '45' (length=2)
it should be
array
'UID' => string '45' (length=2)
update*
what about the 0 ? where does it came from ? thanks for the replies.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有指定
fetch_style
参数。它默认返回FETCH_BOTH
,这是一个数组。以下是选项以及如何指定它:http://php.net/manual/en/pdostatement.fetch.php
编辑:此外,即使只有一列,它也总是返回一个数组,因为一行可以包含多个属性。您可以使用
FETCH_ASSOC
,然后指定列名称来获取数据,或者,如果您像您一样使用fetch()
,则数组将按列进行索引名称和 0 索引的列号。You didn't specify a
fetch_style
parameter. It returnsFETCH_BOTH
by default, which is an array. Here are the options and how to specify it:http://php.net/manual/en/pdostatement.fetch.php
EDIT: Also, it will always return an array, even if there's only one column, because a row can contain multiple attributes. You can use
FETCH_ASSOC
and then specify your column name to get the data, or, if you just usefetch()
like you did, the array is indexed by both the column name and the 0-indexed column number.如果您只想获取列,则需要 fetchColumn() PDOStatement 的方法。
If you want to get just the column, you need the fetchColumn() method of PDOStatement.
结果集是逐行获取的,即使该行包含单个列
The resultset is fetched line by line, even if the line contains a single column