使用PDO匹配数据库中的行时遇到问题 [英] Having issue with matching rows in the database using PDO
问题描述
我正在寻找行数,以检查数据库中是否已存在同一封电子邮件.我尝试了几种机制,但没有成功.当我直接在数据库中运行查询时,它为我提供了行数,但是通过PDO执行时,它为我提供了0.
I am looking to get row count to check if same email is already in database or not. i have tried couple of mechanism but no success. when i run my query directly in the database it gives me the row count but via PDO execute it gives me 0.
我使用了fetchAll方法进行手动计数,甚至使用了行计数方法也不起作用
i have used fetchAll method to manually count, even used rowCount method that also not working
$sql = 'SELECT count(*) FROM inbox WHERE uid = "'.$email_number.'" AND from_email = "'.$email_f.'"';
$result = $link->prepare($sql);
$result->execute();
$number_of_rows = $result->fetchColumn();
问题与此$ email_f一起使用,它包含html
issue is with this $email_f, it contains html
SELECT count(*) FROM inbox WHERE uid = "6961"
AND from_email = "abc Offers <abc@abcs.com>"
这是我从$ sql打印的查询,当我在phpmyadmin中直接在数据库中执行该查询时,它工作正常.给我3的数量,但是通过执行我得到0.
this is the query which i have printed from $sql and when i execute it in database directly in phpmyadmin, it works fine. give me count of 3 but via execute i get 0.
推荐答案
最有可能在输入数据(或数据库)中存在一些已转换或不可打印的字符.例如,可能存在换行符或特殊编码的符号,或者某些字符(例如<
和>
)转换为HTML实体.结果,查询中包含<abc@abcs.com>
的文本将永远不会与文本<abc@abcs.com>
匹配.
Most likely there are some converted or non-printable characters in the input data (or database). For example, there could be a linefeed character or a peculiarly encoded symbol, or some characters such as <
and >
converted into HTML entities. As a result, the query contains <abc@abcs.com>
will never match a text <abc@abcs.com>
.
问题是,这只是一个猜测,没有人能告诉您实际的问题是什么,因为它是您的数据库,您的输入数据并且只有您可以找到问题.
The problem is, this is only a guess, and nobody can tell you what the actual issue is, because it is your database, your input data and only you can find the issue.
我写了一篇文章,解释了如何调试PDO问题.
I wrote an article that explains how to debug your PDO issues.
要调试特定问题,您需要
To debug a particular issue, you need
- 确保同时为PDO和PHP启用了完整的错误报告.确实有帮助,向您显示偶发的印刷错误,拼写错误等
- 仔细检查数据库中的数据和输入以发现差异.
bin2hex()
函数将有助于显示数据库和输入中的所有不可打印和转换的字符.
- make sure the full error reporting is enabled for both PDO and PHP. It really helps, showing you occasional typographic errors, spelling errors and the such
- scrutinize both the data in the database and the input to find the difference.
bin2hex()
function would help, revealing all non-printable and converted characters, in both database and the input.
还有一点,但还是非常重要:您准备好的语句是货物崇拜代码, 什么也不保护.这是必须的方式:
On a side note, but very important nevertheless: your prepared statement is a cargo cult code that protects nothing. Here is how it must be:
$sql = 'SELECT count(*) FROM inbox WHERE uid = ? AND from_email = ?';
$result = $link->prepare($sql);
$result->execute([$email_number,$email_f]);
$number_of_rows = $result->fetchColumn();
这篇关于使用PDO匹配数据库中的行时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!