PEAR DB 准备/绑定值

发布于 2024-09-25 14:21:18 字数 985 浏览 3 评论 0原文

只是似乎无法在不执行查询的情况下打印绑定值。希望在执行之前调试查询。有什么建议吗?我知道我忽略了一些简单的事情,呃...

$field1 = 'one';
$field2 = 'two';
$field3 = 'three';

$fields  = 'SET ';
$fields .= 'field1 = ?, ';
$fields .= 'field2 = ?, ';
$fields .= 'field3 = ? ';

$vals[] = $field1;
$vals[] = $field2;
$vals[] = $field3;

$sql = 'UPDATE table_name '.$fields.' WHERE id = 123';
$dbh = $db->prepare($sql);

// this binds and executes the query but I would like to print the query with the bind values before executing
$results = $db->execute($dbh, $vals); 

更新:

我会用 sprinf 做这样的事情

$field1 = 'one';
$field2 = 'two';
$field3 = 'three';

$fields  = 'SET ';
$fields .= 'field1 = %s, ';
$fields .= 'field2 = %s, ';
$fields .= 'field3 = %s ';

$vals[] = $field1;
$vals[] = $field2;
$vals[] = $field3;

$sql = 'UPDATE table_name '.$fields.' WHERE id = 123';

$query = sprintf($sql, $field1, $field2, $field3);
echo "Query before execution: ".$query."<br />";

Just can't seem to print the binded values without executing the query. Looking to debug the query before execution. Any tips? I know I'm overlooking something simple, ugh...

$field1 = 'one';
$field2 = 'two';
$field3 = 'three';

$fields  = 'SET ';
$fields .= 'field1 = ?, ';
$fields .= 'field2 = ?, ';
$fields .= 'field3 = ? ';

$vals[] = $field1;
$vals[] = $field2;
$vals[] = $field3;

$sql = 'UPDATE table_name '.$fields.' WHERE id = 123';
$dbh = $db->prepare($sql);

// this binds and executes the query but I would like to print the query with the bind values before executing
$results = $db->execute($dbh, $vals); 

UPDATE:

I would do something like this with sprinf

$field1 = 'one';
$field2 = 'two';
$field3 = 'three';

$fields  = 'SET ';
$fields .= 'field1 = %s, ';
$fields .= 'field2 = %s, ';
$fields .= 'field3 = %s ';

$vals[] = $field1;
$vals[] = $field2;
$vals[] = $field3;

$sql = 'UPDATE table_name '.$fields.' WHERE id = 123';

$query = sprintf($sql, $field1, $field2, $field3);
echo "Query before execution: ".$query."<br />";

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

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

发布评论

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

评论(1

倒数 2024-10-02 14:21:18

您无法像这样获取查询中的值。服务器处理准备好的查询的方式是不同的。

您可以做的最好的事情是:

echo $sql;
print_r($vals);

检索(或模拟)完整查询来自 PDO 准备好的语句

You can't get the values inside the query like that. The way the server handles prepared queries is different.

The best you could do is:

echo $sql;
print_r($vals);

Retrieve (or simulate) full query from PDO prepared statement

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