从 PDO 准备好的语句中获取查询

发布于 2024-08-21 12:10:53 字数 33 浏览 5 评论 0原文

有没有办法检索用于生成 PDO 准备语句对象的查询?

Is there a way to retrieve the query that was used to generate a PDO Prepared statement object?

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

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

发布评论

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

评论(4

神经暖 2024-08-28 12:10:53

实现您想要的最简单的方法是:

$statement->debugDumpParams();

只需确保在执行语句后添加它即可。

The simplest way to achieve what you want is:

$statement->debugDumpParams();

Just make sure you add it after executing the statement.

风尘浪孓 2024-08-28 12:10:53

如果您不反对扩展默认的 \PDO 和 \PDOStatement 对象,您可以考虑查看:

github.com/noahheck/E_PDOStatement

PDO 的此扩展允许您查看完整的查询语句,作为可能在数据库级别执行的示例。它使用正则表达式来插入 PDO 语句的绑定参数。

通过扩展默认的 \PDOStatement 定义,E_PDOStatement 能够为默认功能提供此增强功能,而无需修改正常工作流程。

免责声明:我创建了此扩展程序。

我只是希望它对其他人有帮助。

If you aren't opposed to extending the default \PDO and \PDOStatement object, you might consider looking at:

github.com/noahheck/E_PDOStatement

This extension to PDO allows you to see a full query statement as an example of what might be executed at the database level. It uses regex to interpolate the bound parameters of your PDO statement.

By extending the default \PDOStatement definition, E_PDOStatement is able to offer this enhancement to the default functionality without requiring modification to your normal work flow.

Disclaimer: I created this extension.

I just hope it's helpful to someone else.

╄→承喏 2024-08-28 12:10:53

这个程序有效。由于 debugDumpParams() 不返回输出。这是我设计的一个小技巧。

// get the output before debugDumpParams() get executed 
$before = ob_get_contents();

//start a new buffer
ob_start();

// dump params now
$smt->debugDumpParams();

// save the output in a new variable $data
$data = ob_get_contents();

// clean the output screen
ob_end_clean();

// display what was before debugDumpParams() got executed
printf("%s", $before);

$statement = "";

// Now for prepared statements
if (stristr($data, 'Sent SQL') !== false)
{

// begin extracting from "Sent SQL"
$begin = stristr($data, 'Sent SQL');

// get the first ] square bracket
$square = strpos($begin, "]");

// collect sql
$begin = substr($begin, $square + 1);
$ending = strpos($begin, "Params");

$sql = substr($begin, 0, $ending);
$sql = trim($sql);

  // sql statement here
  $statement = $sql;
}
else
{
  if (stristr($data, 'SQL') !== false)
  {
     $begin = stristr($data, 'SQL');
     // get the first ] square bracket
     $square = strpos($begin, "]");

     // collect sql
     $begin = substr($begin, $square + 1);
     $ending = strpos($begin, "Params");

     $sql = substr($begin, 0, $ending);
     $sql = trim($sql);

     $statement = $sql;
  }

}


// statement here
echo $statement;

希望这有帮助。

This procedure works. Since debugDumpParams() doesn't return the output. Here is a little trick i designed.

// get the output before debugDumpParams() get executed 
$before = ob_get_contents();

//start a new buffer
ob_start();

// dump params now
$smt->debugDumpParams();

// save the output in a new variable $data
$data = ob_get_contents();

// clean the output screen
ob_end_clean();

// display what was before debugDumpParams() got executed
printf("%s", $before);

$statement = "";

// Now for prepared statements
if (stristr($data, 'Sent SQL') !== false)
{

// begin extracting from "Sent SQL"
$begin = stristr($data, 'Sent SQL');

// get the first ] square bracket
$square = strpos($begin, "]");

// collect sql
$begin = substr($begin, $square + 1);
$ending = strpos($begin, "Params");

$sql = substr($begin, 0, $ending);
$sql = trim($sql);

  // sql statement here
  $statement = $sql;
}
else
{
  if (stristr($data, 'SQL') !== false)
  {
     $begin = stristr($data, 'SQL');
     // get the first ] square bracket
     $square = strpos($begin, "]");

     // collect sql
     $begin = substr($begin, $square + 1);
     $ending = strpos($begin, "Params");

     $sql = substr($begin, 0, $ending);
     $sql = trim($sql);

     $statement = $sql;
  }

}


// statement here
echo $statement;

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文