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'),

    $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):

    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';
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 python使用pulp线性优化时报错
  • ¥15 开源或低价数据中台哪个最好
  • ¥15 arduino编程出现字符串疑似覆盖现象
  • ¥15 我的b站在没有碰到屏幕的情况下偶尔会自动跳出进度条,就像在屏幕上点了一下一样,但我并没有点。而且视频进度并没有变。这可能是什么原因造成的?
  • ¥30 STK matlab python仿真
  • ¥15 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题