PDO调试 - 查看查询后绑定? [英] PDO Debugging - View Query AFTER Bind?

查看:96
本文介绍了PDO调试 - 查看查询后绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

从PDO准备语句中检索(或模拟)完整查询

我不知道为什么我的查询返回0行..它实现了一些非常动态的搜索功能,以及很多if / loop语句等。为了进行调试,我会喜欢看到正在发送到服务器的字符串。有没有办法通过PHP?

I can't figure out why my query is returning 0 rows.. it implements some very dynamic search functionality, and a lot of if/loop statements etc. Therefor to debug it, I'd like to see EXACTLY what string is being sent to the server. Is there a way to do this through PHP?

有没有办法问服务器最后一个查询是什么,或者告诉PDO告诉我你是什么发送?

Is there maybe a way to ask the server "what was the last query", or tell PDO "show me what you sent"?

我看到一个回复​​,使用 str_replace 手动输入值代替:fieldValue ,但它可能是一个语法问题(或者可能是通过一个不正确的循环等),这种方法对此无帮助。

I saw one response using str_replace to manually enter the values in place of :fieldValue, but it's likely a syntax problem (or maybe it's going through an incorrect loop, etc), which this method doesn't help with.

使用 bindValue(:fieldValue,$ value); 如果有所作为。

编辑

结果是一个简单的 if($ var =true){... 应该是 if($ var ==true){... 。 PHP我觉得在这个意义上与Java不一样?无论哪种方式,问题仍然存在(我经常遇到这样的情况)。我不得不使用一系列 echo你在这里; 找到这个错误,因为它在技术上是有效的但不正确的。如果我有最后的SQL语句,我可以看到哦,我的代码添加了 where column = true ,必须经历错误的IF ...。

Turns out it was a simple if ($var="true") { ... which should have been if ($var=="true") { .... PHP I guess is not the same as Java in that sense? Either way, the question still stands (as I run into this often). I had to use a series of echo "You are Here"; to find this error, as it was technically valid but not correct. If I had the final SQL statement, I could have seen "Oh, my code has added the where column = true, must have gone through the wrong IF...".

推荐答案

这是关于SQL调试的唯一最常见的神话。 我需要在准备后看到查询,以便能够判断是否发生错误。事实是,你不,我会告诉你为什么。

That's the single most common myth about SQL debugging. "I need to see the query after preparation to be able to tell if an error occurred". The fact is, you don't, and I'll tell you why.

一旦查询准备好,占位符可以被认为是有效的字符串/整数。你不在乎它的内容。

Once a query has been prepared, the placeholder can be considered as a valid string/integer. You don't care what's in it.

另外,如果你正确设置PDO,你会得到一个详细的 PDOException 详细说明了错误发生的完整回溯的错误,再加上您从MySQL获取错误字符串,这使得语法错误很容易找到。

Also, if you set up PDO correctly, you'll get a detailed PDOException detailing the error you've had along with a complete backtrace of where the error happened, plus you get the error string from MySQL, which makes syntax errors very easy to find.

要启用PDO异常并禁用仿真准备:

To enable PDO Exceptions and disable emulated prepares:

$pdo = new PDO("mysql:host=localhost;dbname=database_name", "user", "password");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

这篇关于PDO调试 - 查看查询后绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆