栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

获取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() );


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/466043.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号