duanqin9631 2013-07-17 21:31
浏览 31

bindParam在哪里存储绑定?

When using either bindParam or bindValue in PDO, where exactly is the "binding" stored in the PDOStatement object? Is it possible to display this set property/reference/etc.?

I'm trying to understand where these and other PDOStatement methods store/locate their data within a PDOStatement object (if it's even possible to access it outside of setting it through a PDOStatement method).

(I suppose this question extends to other PDO/PDOStatement methods, as well, curious where things are getting stored, like if into properties or whatnot)

  • 写回答

1条回答 默认 最新

  • dongle19863 2013-07-17 21:43
    关注

    A prepared statement is either a protocol feature (mysql for example) or will run in a C library behind the scenes. Meaning the work of parsing the statement isn't done by PDO code. It is done by the database server or a C library like libsqlite. As PDO doesn't parse the statement by itself and just passes it to the lower level component, param information isn't available to it.

    So, using plain PHP it is not possible to get the association placeholder => replaced by value in query using the PDOStatement object. This is by design of a prepared statement.

    A workaround might be to extend PDOStatement and add a method that replaces param placeholders by their values in the current query string. I have prepared an example below. Note that this example isn't bullet-proof as it would for example replace :bar even if it occurs in quoted strings. But for internal usage, a solution like this has done a good job for me so far.

    Custom Statement Class:

    class MyStatement extends PDOStatement
    {
    
        protected $params;
        protected $pdo;
    
        protected function __construct($pdo) {
            $this->pdo = $pdo;
        }
    
        public function execute($params = null) {
            $this->params = $params;
            return parent::execute($params);
        }
    
    
        public function printQuery(){
            $_params = $this->params;
            $sql = $this->queryString;
            foreach($this->params as $key => $value) {
                $_value = is_null($value) ?
                    'NULL' : '\'' . $value . '\'';
                $sql = str_replace(':'. $key, $_value, $sql);
            }
            return $sql;
        }
    }
    

    You need a modified PDO:

    class MyPDO extends PDO
    {
    
        public function __construct($dsn, $username="", $password="", $driver_options=array()) {
            parent::__construct($dsn, $username, $password, array(
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                PDO::ATTR_STATEMENT_CLASS => array('MyStatement', array($this))
            ));
        }
    }
    

    Test Code:

    $pdo = new MyPDO('mysql:host=localhost;dbname=test', 'root', '******');
    
    // preapre stupid query
    $stmt = $pdo->prepare('SELECT FROM `foo` WHERE name = :bar');
    
    try {
        // execute stmt
        $stmt->execute(array('bar' => 'hek2mgl'));
    } catch (PDOException $e) {
        echo $stmt->printQuery();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP