2011-11-12 72 views
1

我想從PDOStatement獲取最後一個查詢(用於調試目的)。但我無法重寫bindParam和bindValue方法。當我嘗試下面的代碼,我得到:PHP,PDO,最後一個查詢,bindParam

Fatal error: Default value for parameters with a class type hint can only be NULL

然後我更換了PDO::PARAM_STRnull在bindParam/bindValue的參數列表,所以我就:

Strict Standards: Declaration of DBStatement::bindParam() should be compatible with that of PDOStatement::bindParam()

然後我之前被刪除的int$data_type參數並將其默認值設置爲PDO::PARAM_STR。然後我得到:

Strict Standards: Declaration of DBStatement::bindParam() should be compatible with that of PDOStatement::bindParam() in D:\www\pdotest.php on line 73

(有趣的是,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(因爲$代碼參數):

Notice: A non well formed numeric value encountered in D:\www\pdotest.php on line 21

+0

是的,你可以投下這個問題,但答案在哪裏?你知道嗎?請分享! – gopher

+0

它幫了我:) –

回答

1

不需要使用「INT」類型的$ DATA_TYPE變量,那麼它的工作原理。

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); 
} 
0

的通知您收到似乎是由於這個懸而未決php bug。我會更改重寫的bindParam和bindValue,以便它們與父級的聲明兼容。如果你想設置你自己的默認參數值,你可以在重寫的方法中這樣做。最後,我想簡單地改變異常的建設省略代碼:

throw new DBException($e->getMessage(), NULL, $e->getPrevious(), $this->queryString, $this->parameters);