【发布时间】:2016-11-06 14:41:37
【问题描述】:
我尝试选择关注我的任何站点成员的用户数,但即使数据库中不为空,它也会继续返回 0,否则它会在为空时显示 1
<?php
$db_conn = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME,DB_USERNAME,DB_PASSWORD);
$db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$followings = $db_conn->prepare("SELECT COUNT(*) FROM memberfollow WHERE following = :followMe");
$followings->bindParam(":followMe", $_POST['username']);
$followings->execute();
$Numfollowing = $followings->rowCount();//this will return 0 when is empty and not empty
$col = $followings->fetchColumn();// this will return 1 in all
echo "FL".$Numfollowing."/".$col."<br/>";
?>
【问题讨论】:
-
你正在做一个 count(),这意味着你总是会得到至少一行数据。如果哪里没有与您的
where匹配的行,您只会得到一行包含0的行。 -
如何做才能使它正确? @MarcB
-
不要使用计数,选择字段。