使用PDO搜索重复的条目 [英] Searching for duplicate entries with PDO

查看:86
本文介绍了使用PDO搜索重复的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了麻烦,需要一些代码来查找平台名称的重复项.以后也可以使用它来查找唯一的ID.

I'm having a spot of trouble with a bit of code meant to find duplicates of a name along with the platform. This will also be adapted to find unique IDs later on.

因此,例如,如果Xbox上有一个名为"Apple"的服务器,而您尝试在同一平台上插入一个名称为"Apple"的记录,它将拒绝该记录.但是,允许使用具有相同名称的另一个平台,例如带有PS3的"Apple".

So for example, if there is a server named "Apple" on the Xbox and you try to insert a record with the name "Apple" with the same platform it will reject it. However, another platform with the same name is allowed, such as "Apple" with PS3.

我已经尝试过提出想法并寻找答案,但是对于检查重复项的最佳方法,我还是一无所知.

I've tried coming up with ideas and searching for answers, but I'm kind of in the dark as to what is the best way to go about checking for duplicates.

到目前为止,这就是我所拥有的:

So far this is what I have:

$nameDuplicate_sql = $db->prepare("SELECT * FROM `servers` WHERE name=':name' AND platform=':platform'");
$nameDuplicate_sql->bindValue(':name', $name);
$nameDuplicate_sql->bindValue(':platform', $platform);
$nameDuplicate_sql->execute();

我尝试了很多不同的解决方案,其中一些来自这里,其他来自PHP手册等等.

I've tried a bunch of different solutions, some from here, others from the PHP's manual and etc. None appear to work though.

我试图坚持使用PDO,但是在这种情况下,我无法确定转弯的位置.如果这是在mysql_ *中,我可能只能使用mysql_affected_rows,但是对于PDO,我却一无所知. rowCount看起来很有希望,但由于它既不是INSERT,UPDATE或DELETE语句,因此始终返回0.

I'm trying to stick with PDO, however, this is one instance where I cannot figure out where to turn. If this was in mysql_* I probably could just use mysql_affected_rows, but with PDO I have no clue. rowCount seemed promising, but it always returns 0 since this is neither an INSERT, UPDATE, or DELETE statement.

哦,我已经在phpMyAdmin中尝试了SQL语句,它可以工作;我用一个简单的名称/平台进行了尝试,并正确地找到了行.

Oh, and I've tried the SQL statement in phpMyAdmin and it works; I tried it with a simple name/platform and it found rows properly.

如果有人可以在这里帮助我,我将不胜感激.

If anyone can help me out here I'd appreciate it.

推荐答案

对于大多数数据库,PDOStatement :: rowCount()不会返回 SELECT语句影响的行数. 相反,请使用PDO :: query()发出带有与预期SELECT语句相同的谓词的SELECT COUNT(*)语句,然后使用 PDOStatement :: fetchColumn()检索将要行数 被退回. 然后,您的应用程序可以执行正确的操作.

For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action.

这篇关于使用PDO搜索重复的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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