我正在尝试使用 PHP PDO 创建一个 Setting
对象的新实例调用 __construct()
方法并约束 PDO::FETCH_PROPS_LATE.不幸的是,我收到了这个警告(并且绑定不起作用).
如何将列值传递给构造函数方法?
<块引用>警告:pdo.php 中的 Setting::__construct() 缺少参数 1.
注意:未定义变量:pdo.php 中的键.
类设置{受保护的 $key, $value, $displayable;公共函数 __construct($key, $value = null, $displayable = 1){$this->key = $key;$this->value = $value;$this->displayable = $displayable >0;}}while($mashup = current($mashup)){$stmt = $dbh->prepare('SELECT `key`, value, displayable从设置 WHERE mashup_id = :id');$stmt->bindParam(':id', $mashup->id, PDO::PARAM_INT);$stmt->execute();$settings = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,'环境');}$stmt->closeCursor();
您的构造函数中有一个非默认参数 $key
:
public function __construct($key, $value = null, $displayable = 1)
所以,当你这样做时:
$settings = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,'Setting');
错误:warning: Missing argument 1 for Setting::__construct() in pdo.php
仅针对参数 $key
抛出,因为它不是默认值.>
正确使用fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,...
是这样的:
$variable = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,'班级名称',<构造函数中使用的参数名称数组(按顺序)>);
所以,就你而言:
$variable = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,'环境',array('key', 'value', 'displayable');
I'm trying to create a new instance of Setting
object calling __construct()
method with PHP PDO and constrain PDO::FETCH_PROPS_LATE. Unfortunatly i'm getting this warning (and binding doesn't work).
How can pass column values to the constructor method?
Warning: Missing argument 1 for Setting::__construct() in pdo.php.
Notice: Undefined variable: key in pdo.php.
class Setting
{
protected $key, $value, $displayable;
public function __construct($key, $value = null, $displayable = 1)
{
$this->key = $key;
$this->value = $value;
$this->displayable = $displayable > 0;
}
}
while($mashup = current($mashups))
{
$stmt = $dbh->prepare('SELECT `key`, value, displayable
FROM setting WHERE mashup_id = :id');
$stmt->bindParam(':id', $mashup->id, PDO::PARAM_INT);
$stmt->execute();
$settings = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,
'Setting');
}
$stmt->closeCursor();
You have a non defaulted parameter $key
in your constructor:
public function __construct($key, $value = null, $displayable = 1)
So, when you are doing this:
$settings = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,'Setting');
Error: warning: Missing argument 1 for Setting::__construct() in pdo.php
is thrown only for parameter $key
because it is not defaulted.
The correct use of fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,...
is like this:
$variable = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,
'classname',
<array of parameter names(in order) used in constructor>);
So, in your case:
$variable = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,
'Setting',
array('key', 'value', 'displayable');
这篇关于PDO 使用 PDO::FETCH_PROPS_LATE 和 __construct() 调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!