具有PDO查询的SQL表中的项



我有一个库的SQL表,它有id、userid和imagename行。我正在尝试编写一个函数来计算某个用户添加了多少图像,我是PDO和SQL的新手。这就是我的职能;

// Photo Count
public function photoCount($id){
$this->db->query('SELECT * FROM gallery WHERE id = :id');
// Bind value
$this->db->bind(':id', $id);
$row = $this->db->single();
// Check row
$count = $this->db->rowCount();
return $count;  
}

我需要$row = $this->db->single();行吗?还是我现在只返回rowCount?返回的都是0,这是不正确的。感谢您的帮助。

使用SQL计数函数。如果$this->db是PDO对象,那么。。。

public function photoCount($id){
$statement = $this->db->prepare('SELECT count(*) as c FROM gallery WHERE id = :id');
$statement->bindParam(":id", $id);
$statement->execute();
$result = $statement->fetch(PDO::FETCH_OBJ);
return $result->c;
}

最新更新