PEAR DB 准备/绑定值
只是似乎无法在不执行查询的情况下打印绑定值。希望在执行之前调试查询。有什么建议吗?我知道我忽略了一些简单的事情,呃...
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法像这样获取查询中的值。服务器处理准备好的查询的方式是不同的。
您可以做的最好的事情是:
检索(或模拟)完整查询来自 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:
Retrieve (or simulate) full query from PDO prepared statement