PHP,PDO,最后一个查询,bindParam [英] PHP, PDO, last query, bindParam
问题描述
我想从PDOStatement获得最后一个查询(用于调试目的)。但我无法覆盖bindParam和bindValue方法。当我尝试下面的代码时,我得到:
致命错误:具有类类型提示的参数的默认值只能为NULL
然后我将 PDO :: PARAM_STR
替换为 null
在bindParam / bindValue的参数列表中,所以我得到了:
严格标准:DBStatement声明:: bindParam()应该与PDOStatement :: bindParam()的兼容性
然后删除 int
在 $ data_type
参数之前,并将默认值设置为 PDO :: PARAM_STR
。然后我得到了:
严格标准:DBStatement :: bindParam()的声明应该与PDOStatement :: bindParam()的声明兼容第73行的D:\\\ n \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
那么,我现在该怎么办?
class DBConnection扩展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);
}
}
类DBException扩展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;
}
公共函数getQueryString()
{
返回$ this-> query_string;
}
公共函数getParameters()
{
返回$ this->参数;
}
}
类DBStatement扩展PDOStatement
{
private $ conn;
private $ parameters = array();
受保护的函数__construct($ conn)
{
$ this-> conn = $ conn;
}
公共函数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);
}
公共函数bindValue($ parameter,$ value,int $ data_type = PDO :: PARAM_STR)
{
$ this-> parameters [$ parameter ] = $ value;
parent :: bindValue($ parameter,$ value,$ data_type);
}
公共函数执行($ input_parameters = null)
{
try
{
parent :: execute($ input_parameters);
}
catch(PDOException $ e)
{
抛出新的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查询字符串为:。$ e-> getQueryString()。\ n;
echo参数是:。print_r($ e-> getParameters(),true);
}
当我抛出DBException时,我也收到了以下内容(因为$ code参数) ):
注意:在第21行的D:\\\ pdotest.php中遇到一个格式不正确的数值
解决方案不需要使用$ data_type变量的int类型,然后它可以工作。
公共函数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);
}
公共函数bindValue($ parameter,$ value,$ data_type = PDO :: PARAM_STR)
{
$ this-> parameters [$ parameter] = $ value;
parent :: bindValue($ parameter,$ value,$ data_type);
}
I would like to have the last query (for debugging purposes) from the PDOStatement. But I can't override the bindParam and bindValue methods. When I tried the code below, I got:
Fatal error: Default value for parameters with a class type hint can only be NULL
Then I replaced the
PDO::PARAM_STR
tonull
in the parameter list of bindParam/bindValue, so I got:Strict Standards: Declaration of DBStatement::bindParam() should be compatible with that of PDOStatement::bindParam()
I then deleted
int
before the$data_type
parameter and set the default value toPDO::PARAM_STR
. Then I got:Strict Standards: Declaration of DBStatement::bindParam() should be compatible with that of PDOStatement::bindParam() in D:\www\pdotest.php on line 73
(interestingly, the bindValue is okay now).
So, what can I do now?
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); }
I also recieve te following when I throw a DBException (because of $code parameter):
Notice: A non well formed numeric value encountered in D:\www\pdotest.php on line 21
解决方案Don't need to use the "int" type of the $data_type variable, then it works.
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); }
这篇关于PHP,PDO,最后一个查询,bindParam的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!