如何获取 Pear MDB2 生成的 SQL 而不执行它?

发布于 2024-11-14 16:31:42 字数 612 浏览 3 评论 0原文

我正在使用 Pear MDB2 和 PHP 5.3。我正在编写一个更新数据库的项目,在让它开始更改数据之前,我想在实际执行它们之前看看 autoPrepare() 和execute() 生成的 SQL 查询是什么样的。

我计划创建并执行这样的更新查询:

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

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

我已经知道我可以通过访问 $stmt->query< 查看由 autoPrepare() 生成的 SQL,其中包含值的占位符/代码>。我希望看到由 execute() 生成的完整 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

第七度阳光i 2024-11-21 16:31:42

准备好的语句在服务器端编译,因此在执行之前您无法看到它们。例如,在 MySQL 中,如果您想执行准备好的语句,MDB2 实际上做的是:

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

服务器永远不会“返回”它执行的实际查询。如果您想查看执行了哪些查询,则必须设置查询日志。

例如,在 MySQL (my.cnf) 中:

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

对于上面的查询示例,查询日志将显示:

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';

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';
夏九 2024-11-21 16:31:42
print_r ($db->last_query, true);
print_r ($db->last_query, true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文