我想从PDOStatement获得最后一个查询(用于调试目的)。但是我无法覆盖bindParam和bindValue方法。当我尝试下面的代码时,我得到了:
致命错误:具有类类型提示的参数的默认值只能为NULL
然后我在bindParam / bindValue的参数列表中将PDO::PARAM_STR
替换为null
,所以我得到了:
严格标准:DBStatement :: bindParam()声明应与PDOStatement :: bindParam()的声明兼容
然后我在int
参数之前删除了$data_type
,并将默认值设置为PDO::PARAM_STR
。然后我得到了:
严格标准:DBStatement :: bindParam()声明应与第73行D:\ www \ pdotest.php中PDOStatement :: bindParam()的声明兼容
(有趣的是,现在bindValue还可以。)
那么,我现在该怎么办?
class DBConnection extends PDO
{
public function __construct($dsn, $username = null, $password = null, $driver_options = array())
{
parent::__construct($dsn, $username, $password, $driver_options);
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('DBStatement', array($this)));
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
}
class DBException extends PDOException
{
private $query_string;
private $parameters;
public function __construct($message = '', $code = 0, $previous = null, $query_string = '', $parameters = array())
{
parent::__construct($message, $code, $previous);
$this->query_string = $query_string;
$this->parameters = $parameters;
}
public function getQueryString()
{
return $this->query_string;
}
public function getParameters()
{
return $this->parameters;
}
}
class DBStatement extends PDOStatement
{
private $conn;
private $parameters = array();
protected function __construct($conn)
{
$this->conn = $conn;
}
public function bindParam($parameter, &$variable, int $data_type = PDO::PARAM_STR, int $length = null, $driver_options = null)
{
$this->parameters[$parameter] = $variable;
parent::bindParam($parameter, $variable, $data_type, $length, $driver_options);
}
public function bindValue($parameter, $value, int $data_type = PDO::PARAM_STR)
{
$this->parameters[$parameter] = $value;
parent::bindValue($parameter, $value, $data_type);
}
public function execute($input_parameters = null)
{
try
{
parent::execute($input_parameters);
}
catch (PDOException $e)
{
throw new DBException($e->getMessage(), $e->getCode(), $e->getPrevious(), $this->queryString, $this->parameters);
}
}
}
$id = 1;
try
{
$conn = new DBConnection('mysql:host=localhost;dbname=test', 'root', '');
$stmt = $conn->prepare('select * from foo where id = :id');
$stmt->bindParam(':id', $id);
$stmt->execute();
}
catch (DBException $e)
{
echo "Query string was: ".$e->getQueryString()."\n";
echo "Parameters was: ".print_r($e->getParameters(), true);
}
当我抛出DBException时(因为$ code参数),我也收到了以下内容:
注意:第21行的D:\ www \ pdotest.php中遇到一个格式不正确的数值
答案 0 :(得分:1)
不需要使用$ data_type变量的“int”类型,然后它可以工作。
public function bindParam($parameter, &$variable, $data_type = PDO::PARAM_STR, $length = null, $driver_options = null)
{
$this->parameters[$parameter] = $variable;
parent::bindParam($parameter, $variable, $data_type, $length, $driver_options);
}
public function bindValue($parameter, $value, $data_type = PDO::PARAM_STR)
{
$this->parameters[$parameter] = $value;
parent::bindValue($parameter, $value, $data_type);
}
答案 1 :(得分:0)
您收到的通知似乎是由于此未解决php bug。我会更改重写的bindParam和bindValue,以便它们与父的声明兼容。如果要设置自己的默认参数值,可以在重写的方法中执行此操作。最后,我只是更改异常的构造以省略代码:
throw new DBException($e->getMessage(), NULL, $e->getPrevious(), $this->queryString, $this->parameters);