draxu26480 2011-06-08 19:44
浏览 57
已采纳

如何在不执行的情况下获取Pear MDB2生成的SQL?

I'm working with Pear MDB2 with PHP 5.3. I'm coding a project that updates a DB and before I let it start changing data, I'd like to see what the SQL queries generated by autoPrepare() and execute() look like before actually executing them.

I plan to create and execute an update query like this:

    $stmt = $db->extended->autoPrepare($tableName, $tableColumns,
    MDB2_AUTOQUERY_UPDATE, 'id = ' . $db->quote(12345, 'integer'),
    $tableColumnTypes));

    $res =& $stmt->execute($tableColumnValues);

I already know that I can see the SQL generated by autoPrepare() with placeholders for the values by accessing $stmt->query. I'd like to see the completed SQL generated by execute(), with values substituted for placeholders, without actually sending the query to the DB.

How can I do that?

  • 写回答

2条回答 默认 最新

  • douliao7354 2011-06-08 19:54
    关注

    Prepared statements are compiled on the server-side, so you can't see them before they execute. Per example, in MySQL, if you want to execute a prepared statement, what MDB2 actually does is:

    PREPARE stmt FROM 'SELECT * FROM foo WHERE bar = ?';
    SET @baz = 'baz';
    EXECUTE stmt USING @baz;
    

    The server never "returns" the actual query it executed. If you want to see what query was executed, you'll have to set-up a query log.

    Per example, in MySQL (my.cnf):

    [mysqld]
    general_log_file = /var/log/mysql_queries.log
    general_log = 1
    

    The query log would show, for the query example above:

    Query     PREPARE stmt FROM 'SELECT * FROM foo WHERE bar = ?';
    Query     SET @baz = 'baz';
    Query     EXECUTE stmt USING @baz;
    Execute   SELECT * FROM foo WHERE bar = 'baz';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 脱敏项目合作,ner需求合作
  • ¥15 脱敏项目合作,ner需求合作
  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密