Getting raw SQL query string from PDO prepared statements

前端 未结 16 889
心在旅途
心在旅途 2020-11-22 06:56

Is there a way to get the raw SQL string executed when calling PDOStatement::execute() on a prepared statement? For debugging purposes this would be extremely useful.

16条回答
  •  臣服心动
    2020-11-22 07:19

    Somewhat related... if you are just trying to sanitize a particular variable you can use PDO::quote. For example, to search for multiple partial LIKE conditions if you're stuck with a limited framework like CakePHP:

    $pdo = $this->getDataSource()->getConnection();
    $results = $this->find('all', array(
        'conditions' => array(
            'Model.name LIKE ' . $pdo->quote("%{$keyword1}%"),
            'Model.name LIKE ' . $pdo->quote("%{$keyword2}%"),
        ),
    );
    

提交回复
热议问题