我有以下表格和行:
tasks
id name
===================
1 create user
2 delete user
3 update user
我正在尝试获取有关该表中有多少任务的行数。我有以下功能
$stmt = $this->db->prepare("SELECT COUNT(*) as cnt FROM tasks");
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_OBJ)->cnt;
return $result;
它只是保持返回0,但如果我采取相同的查询并直接在数据库上运行它可以正常工作。有谁知道为什么这不起作用?
答案 0 :(得分:0)
尝试像这样简化
$stmt = $this->db->prepare("SELECT COUNT(*) as cnt FROM tasks");
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_OBJ);
return $result->cnt;