获取PHP PDO中最后执行的查询

获取PHP PDO中最后执行的查询,第1张

获取PHP PDO中最后执行查询
<?phpclass MyPDOStatement extends PDOStatement{  protected $_debugValues = null;  protected function __construct()  {    // need this empty construct()!  }  public function execute($values=array())  {    $this->_debugValues = $values;    try {      $t = parent::execute($values);      // maybe do some logging here?    } catch (PDOException $e) {      // maybe do some logging here?      throw $e;    }    return $t;  }  public function _debugQuery($replaced=true)  {    $q = $this->queryString;    if (!$replaced) {      return $q;    }    return preg_replace_callback('/:([0-9a-z_]+)/i', array($this, '_debugReplace'), $q);  }  protected function _debugReplace($m)  {    $v = $this->_debugValues[$m[1]];    if ($v === null) {      return "NULL";    }    if (!is_numeric($v)) {      $v = str_replace("'", "''", $v);    }    return "'". $v ."'";  }}// have a look at http://www.php.net/manual/en/pdo.constants.php$options = array(  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,  PDO::ATTR_STATEMENT_CLASS => array('MyPDOStatement', array()),);// create PDO with custom PDOStatement class$pdo = new PDO($dsn, $username, $password, $options);// prepare a query$query = $pdo->prepare("INSERT INTO mytable (column1, column2, column3)  VALUES (:col1, :col2, :col3)");// execute the prepared statement$query->execute(array(  'col1' => "hello world",  'col2' => 47.11,  'col3' => null,));// output the query and the query with values insertedvar_dump( $query->queryString, $query->_debugQuery() );


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5441538.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存