可捕获的致命错误:在第30行的/home/refined/public_html/refer.php中,类PDOStatement的对象无法转换为字符串 [英] Catchable fatal error: Object of class PDOStatement could not be converted to string in /home/refined/public_html/refer.php on line 30
问题描述
我目前正在构建一个小型且简单的推荐系统.但是,我在尝试返回单个用户拥有多少个推荐人方面遇到了麻烦.新用户注册后,它将存储在称为"referid"的列中,该ID与引用他的用户名的ID相对应.
I currently am building a small and simple referral system. However I am having trouble trying to return how many referrals one single user has. When a new user registers it stores in a column called "referid" and that ID in there correspond to the ID of the username who referred him.
但是,在尝试查找"referid"列中提到该用户ID的次数然后回显它时,出现此错误:
However, upon attempting to find how many times that user's ID is mentioned in the "referid" column and then echoing it, I get this error:
Catchable fatal error: Object of class PDOStatement could not be converted to string in /home/refined/public_html/refer.php on line 30
我不明白是什么原因造成的.我做了一个快速的谷歌,我所能看到的是PDO与字符串或某些东西不兼容.我之前还没有同时使用PHP和MYSQL,所以我不确定如何不"使用PDO.
I cant understand what's actually causing this. I did a quick google and all I could see was that PDO isn't compatible with strings or something. I haven't used PHP and MYSQL together that much before so I'm not sure how to "not" use PDO.
<?php
$checknumber = $odb -> prepare("SELECT COUNT('referid') FROM `users` WHERE `ID` = :ID");
$checknumber -> execute(array(':ID' => $_SESSION['ID']));
echo($checknumber);
?>
非常感谢您的帮助.
推荐答案
让我们分析您的代码:
$checknumber = $odb -> prepare("SELECT COUNT('referid') FROM `users` WHERE `ID` = :ID");
...其中 PDO :: prepare()定义为:
... where PDO::prepare() is defined as:
PDOStatement PDO :: prepare(字符串$ statement [,数组$ driver_options = array()])
PDOStatement PDO::prepare ( string $statement [, array $driver_options = array() ] )
因此它返回一个对象.但是两行之后,您尝试打印该对象:
So it returns an object. But two lines later you try to print the object:
echo($checknumber);
如果您查看手册中的任何用法示例,您会发现您需要调用PDOStatement
方法之一来提取查询结果,例如:
If you check any of the usage examples in the manual you'll see that you need to call one of the PDOStatement
methods to extract the query results, e.g.:
<?php
/* Execute a prepared statement by passing an array of values */
$sql = 'SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour';
$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute(array(':calories' => 150, ':colour' => 'red'));
$red = $sth->fetchAll();
更新:另外两个链接:
- Available PDOStatement methods
- Arrays in PHP
这篇关于可捕获的致命错误:在第30行的/home/refined/public_html/refer.php中,类PDOStatement的对象无法转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!