• <small id='avOC1'></small><noframes id='avOC1'>

    1. <tfoot id='avOC1'></tfoot><legend id='avOC1'><style id='avOC1'><dir id='avOC1'><q id='avOC1'></q></dir></style></legend>

        • <bdo id='avOC1'></bdo><ul id='avOC1'></ul>
        <i id='avOC1'><tr id='avOC1'><dt id='avOC1'><q id='avOC1'><span id='avOC1'><b id='avOC1'><form id='avOC1'><ins id='avOC1'></ins><ul id='avOC1'></ul><sub id='avOC1'></sub></form><legend id='avOC1'></legend><bdo id='avOC1'><pre id='avOC1'><center id='avOC1'></center></pre></bdo></b><th id='avOC1'></th></span></q></dt></tr></i><div id='avOC1'><tfoot id='avOC1'></tfoot><dl id='avOC1'><fieldset id='avOC1'></fieldset></dl></div>

        在 PHP PDO 中获取上次执行的查询

        时间:2023-09-19

              <small id='r4xN9'></small><noframes id='r4xN9'>

            1. <legend id='r4xN9'><style id='r4xN9'><dir id='r4xN9'><q id='r4xN9'></q></dir></style></legend>

                <i id='r4xN9'><tr id='r4xN9'><dt id='r4xN9'><q id='r4xN9'><span id='r4xN9'><b id='r4xN9'><form id='r4xN9'><ins id='r4xN9'></ins><ul id='r4xN9'></ul><sub id='r4xN9'></sub></form><legend id='r4xN9'></legend><bdo id='r4xN9'><pre id='r4xN9'><center id='r4xN9'></center></pre></bdo></b><th id='r4xN9'></th></span></q></dt></tr></i><div id='r4xN9'><tfoot id='r4xN9'></tfoot><dl id='r4xN9'><fieldset id='r4xN9'></fieldset></dl></div>
                <tfoot id='r4xN9'></tfoot>
                  <bdo id='r4xN9'></bdo><ul id='r4xN9'></ul>
                    <tbody id='r4xN9'></tbody>
                • 本文介绍了在 PHP PDO 中获取上次执行的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想知道使用 PHP PDO 执行什么查询.我有:

                  I would like to know what query is executed using PHP PDO. I have:

                  <?php
                  
                  try {  
                    $DBH = new PDO("mysql:host=localhost;dbname=mytable", 'myuser', 'mypass');  
                  }  
                  catch(PDOException $e) {  
                      echo $e->getMessage();  
                  }  
                  
                  $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );  
                  $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  
                  
                  $STH = $DBH->("INSERT INTO mytable (column1, column2, column3 /* etc...*/) value (:column1, :column2, :column3 /* etc...*/)"); 
                  $STH->bindParam(':column1', $column1);  
                  $STH->bindParam(':column2', $column2);  
                  $STH->bindParam(':column3', $column3);  
                   /* etc...*/
                  
                  $STH->execute();  
                  
                  // what is my query?
                  

                  我想得到类似的东西:

                  INSERT INTO mytable (column1, column2, column3) value ('my first column', 32, 'some text')
                  

                  有可能吗?谢谢

                  推荐答案

                  <?php
                  
                  class 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 inserted
                  var_dump( $query->queryString, $query->_debugQuery() );
                  

                  这篇关于在 PHP PDO 中获取上次执行的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何让 php 与 postgresql 一起工作? 下一篇:检查空结果(PHP、PDO 和 MySQL)

                  相关文章

                  最新文章

                • <legend id='asaRC'><style id='asaRC'><dir id='asaRC'><q id='asaRC'></q></dir></style></legend>

                      <bdo id='asaRC'></bdo><ul id='asaRC'></ul>
                    1. <small id='asaRC'></small><noframes id='asaRC'>

                    2. <i id='asaRC'><tr id='asaRC'><dt id='asaRC'><q id='asaRC'><span id='asaRC'><b id='asaRC'><form id='asaRC'><ins id='asaRC'></ins><ul id='asaRC'></ul><sub id='asaRC'></sub></form><legend id='asaRC'></legend><bdo id='asaRC'><pre id='asaRC'><center id='asaRC'></center></pre></bdo></b><th id='asaRC'></th></span></q></dt></tr></i><div id='asaRC'><tfoot id='asaRC'></tfoot><dl id='asaRC'><fieldset id='asaRC'></fieldset></dl></div>
                      <tfoot id='asaRC'></tfoot>