从数据库返回计数

发布于 2024-11-01 10:27:26 字数 906 浏览 0 评论 0原文

我有以下脚本,但它没有像我期望的那样工作:

$DBH = new PDO( "mysql:host=localhost;dbname=database_name", "user", "pass" );
$DBH -> setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

$STH = $DBH -> prepare( "select count( users_id ) from table1 where username = :username" );

$STH -> bindParam( ':username', $_POST['username'], PDO::PARAM_STR, 100 );

$STH -> execute();

$strCount = $STH -> rowCount();

if( $strCount == 1 ) {
    echo $strCount;
    echo "user has already registered, do nothing";
} elseif ( $strCount == 0 ) {
    echo $strCount;
    echo "user has not registered, insert into db";
} else {
    echo $strCount;
    echo "something is wrong, should only ever be 0 or 1";
}

它似乎总是为 $strCount 返回 1

在 table1 中,我只有 1 行,其中用户名是 username1。如果发布的值为 username1,那么我期望 $strCount 为 1,但由于某种原因,如果发布的值为 username2,我仍然得到 $strCount 为 1。

有人知道我做错了什么吗?

I have the following script which is not working as I am expecting it to work:

$DBH = new PDO( "mysql:host=localhost;dbname=database_name", "user", "pass" );
$DBH -> setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

$STH = $DBH -> prepare( "select count( users_id ) from table1 where username = :username" );

$STH -> bindParam( ':username', $_POST['username'], PDO::PARAM_STR, 100 );

$STH -> execute();

$strCount = $STH -> rowCount();

if( $strCount == 1 ) {
    echo $strCount;
    echo "user has already registered, do nothing";
} elseif ( $strCount == 0 ) {
    echo $strCount;
    echo "user has not registered, insert into db";
} else {
    echo $strCount;
    echo "something is wrong, should only ever be 0 or 1";
}

It always seems to return 1 for $strCount

In table1, I just have 1 row where the username is username1. If the posted value is username1, then I expect $strCount to be 1, but for some reason, if the posted value is username2, I am still getting $strCount as 1.

Anyone know what I am doing wrong?

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

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

发布评论

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

评论(3

水染的天色ゝ 2024-11-08 10:27:26

你数了两遍!计数()和行计数()。
PDOStatement::rowCount() 返回受相应 PDOStatement 对象执行的最后一个 DELETE、INSERT 或 UPDATE 语句影响的行数。

尝试进行常规提取

$count = $db->query("select count(*) from table")->fetch(); 

you are doing count twice ! count() and rowcount().
PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

Try doing regualr fetch

$count = $db->query("select count(*) from table")->fetch(); 
﹎☆浅夏丿初晴 2024-11-08 10:27:26

您在查询中执行 COUNT(),这意味着您只能返回一行,其中一个字段包含记录计数。

您必须检索该行和字段,然后根据该值做出决策。

You're doing a COUNT() in the query, which means you'll only ever get a single row back, with one field, containing the count of the records.

You have to retrieve that row and field, then base your decisions on that value.

夏有森光若流苏 2024-11-08 10:27:26

count(blah) 将返回包含计数值的单行。我认为您需要在执行并从那里读取值后调用 fetch(..) ,而不是调用 rowCount() 。这是基于 PHP 文档的。

http://www.php.net/manual/en/pdostatement.fetch.php

我不是 PHP 开发人员,所以我不想给你糟糕的代码。

count(blah) will return a single row with the value of the count. Instead of calling rowCount(), I think you need to call fetch(..) after you execute and read the value from there. This is based on the PHP documentation.

http://www.php.net/manual/en/pdostatement.fetch.php

I'm not a PHP developer so I don't want to give you bad code.

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