php 不能使用 PDOStatement 类型的对象作为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15759223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot use object of type PDOStatement as array
提问by Jony Kale
I want to check if some column of specify user is holding a value higher than 0.
我想检查指定用户的某些列是否持有高于 0 的值。
Problem
问题
When doing the query, and then executing it, Im getting this error:
在执行查询然后执行它时,我收到此错误:
Fatal error: Cannot use object of type PDOStatement as array in C:\xampp\htdocs\recover\admin\create.php on line 40
My code (The query + execute):
我的代码(查询+执行):
if (isset($_SESSION['user'])) {
$admin = $CONNECT_TO_DATABASE->prepare("SELECT * FROM admin WHERE username = :username");
$admin->bindValue(':username', $_SESSION['user']);
$admin->execute();
Line of error (40):
错误行 (40):
if ($settings['create_admins'] > 0 || $admin['super_admin'] > 0 ) {
Question:
题:
Why am I getting this error? How do I fix it?
为什么我收到这个错误?我如何解决它?
I tried doing this:
我尝试这样做:
$admin = $CONNECT_TO_DATABASE->prepare("SELECT * FROM admin WHERE username = :username");
$admin = $admin->bindValue(':username', $_SESSION['user']);
$admin = $admin->execute();
and getting another error:
并收到另一个错误:
Fatal error: Call to a member function execute() on a non-object in C:\xampp\htdocs\recover\admin\create.php on line 38
Thanks!
谢谢!
EDIT: I need the ->fetch object, but I have just done this, and got rid of the errors.. But it doesn't affect? I mean I am echoing that row, and it gives me a null (nothing). Why?
编辑:我需要 -> fetch 对象,但我刚刚完成了这个,并摆脱了错误..但它不影响?我的意思是我在回应那一行,它给了我一个空值(什么都没有)。为什么?
$admin = $CONNECT_TO_DATABASE->prepare("SELECT * FROM admin WHERE username = ".$_SESSION['user']."");
$admin = $admin->fetch();
回答by Shoe
From:
从:
$admin = $CONNECT_TO_DATABASE->prepare("SELECT * FROM admin WHERE username = :username");
[...]
if ($settings['create_admins'] > 0 || $admin['super_admin'] > 0 ) {
$adminis of type PDOStatamentwhich is a class and not an array. Therefore you can't call the []operator on it.
$admin是PDOStatament一个类而不是数组的类型。因此你不能打电话给[]运营商。
Also you should really not alway assign $adminto the return result of every method because most of the PDOStatament's methods return boolean values:
此外,您真的不应该总是分配$admin给每个方法的返回结果,因为大多数PDOStatament方法都返回布尔值:
$admin = $CONNECT_TO_DATABASE->prepare("SELECT * FROM admin WHERE username = :username");
$admin->bindValue(':username', $_SESSION['user']);
$admin->execute();
To retrieve the super_admincolumn from the admintable you should add (after the execute()statement):
要从表中检索super_admin列,admin您应该添加(在execute()语句之后):
$result = $admin->fetch(PDO::FETCH_ASSOC);
which will populate (hopefully, it depends on what's the table schema) $result['super_admin'].
它将填充(希望这取决于表架构是什么)$result['super_admin']。
回答by jerjer
try this:
尝试这个:
$sql = "SELECT * FROM admin WHERE username = ?";
$stmt = $CONNECT_TO_DATABASE->prepare($sql);
$stmt->execute(array($_SESSION['user']));
$admin = $stmt->fetch();
if($admin) {
//do something if query returns row(s)
}

