Php PDO不返回对象,而是返回数组
在下面的代码中,PDO不会将$parcel作为对象返回,而是作为数组返回。当我尝试调用objects函数时,它会失败。我发现的每个例子都是这样做的,方式与我的相似。我做错了什么Php PDO不返回对象,而是返回数组,php,pdo,Php,Pdo,在下面的代码中,PDO不会将$parcel作为对象返回,而是作为数组返回。当我尝试调用objects函数时,它会失败。我发现的每个例子都是这样做的,方式与我的相似。我做错了什么 $statement = $this->connection->query($query); $statement->setFetchMode(PDO::FETCH_CLASS, 'Parcel'); while ($parcel = $statement->fetch
$statement = $this->connection->query($query);
$statement->setFetchMode(PDO::FETCH_CLASS, 'Parcel');
while ($parcel = $statement->fetch()) {
echo $parcel->hello();
}
包裹类,如果有兴趣的话
class Parcel {
public $id;
public $parcel_number;
public $registred_at;
public $shipped_by;
public $shipped_at;
function __construct($parcel_number)
{
$this->parcel_number = $parcel_number;
}
public function hello(){
return "World";
}
}
我使用文件夹结构来构造代码和自动加载程序,这可能会影响PDO调用对象的方式
这是错误代码
Call to a member function info() on a non-object
此错误表示对象为null或未正确实例化,您可能需要指定类的完整路径 比如说
$statement->setFetchMode(PDO::FETCH_CLASS, 'app\model\Parcel');
为了方便起见,您能否显示打印($packet)(在您的while循环中)返回了什么?或者,更好的是,
var\u dump($packet)
say?数组(10){[“id”]=>string(1)“8”[0]=>string(1)“8”[“packet\u number”]=>string(4)“post”[“registered\u at”]=>string(19)“2014-08-03 14:19:50”[2]=>string(19)“2014-08-03 14:19:50”[“发货人”]=>NULL[3]=>NULL[“发货地点”]=>NULL[4]=>NULL}…n