[英]how can i query count in function with parameter that will change according to user?
public function totalmember($designation){
$stmt = $this->pdo->prepare("SELECT COUNT(*) AS totalmember FROM users
WHERE designation = :designation");
$stmt->bindParam(':designation', $designation, PDO::PARAM_INT);
$stmt->execute();
$count = $stmt->fetch(PDO::FETCH_OBJ);
echo $count->totalmember;
}
what i want is when people are logged in their respective account i want people to know how many people from their respective designation(teacher,clerk,peon ) have account?我想要的是当人们登录他们各自的帐户时,我希望人们知道他们各自的职位(老师、职员、员工)有多少人拥有帐户?
Try the fetchAll() function instead of fetch() .尝试fetchAll()函数而不是fetch() 。 Here is the reference link.
这是参考链接。 https://www.php.net/manual/en/pdostatement.fetchall.php
https://www.php.net/manual/en/pdostatement.fetchall.php
Does that work?那样有用吗?
$stmt = $this->pdo->prepare("SELECT * FROM users WHERE designation = :designation");
$stmt->bindParam(':designation', $designation, PDO::PARAM_INT);
$result = $stmt->execute();
$count = $result->rowCount();
echo $count;
$stmt = $this->pdo->prepare("SELECT COUNT(`id`) FROM users WHERE designation = :designation");
$stmt->bindParam(':designation', $designation, PDO::PARAM_INT);
$stmt->execute();
$count = $stmt->fetchColumn();
echo $count;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.