从数据库返回计数
我有以下脚本,但它没有像我期望的那样工作:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你数了两遍!计数()和行计数()。
PDOStatement::rowCount() 返回受相应 PDOStatement 对象执行的最后一个 DELETE、INSERT 或 UPDATE 语句影响的行数。
尝试进行常规提取
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(),这意味着您只能返回一行,其中一个字段包含记录计数。
您必须检索该行和字段,然后根据该值做出决策。
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.
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.