所以我想找一份喜欢的清单。这是我的密码

//Echo total likes of #68
$stmt1 = $con->prepare("SELECT COUNT(*) FROM likes WHERE liked_post_id = :id;");
$stmt1->bindValue(':id', $id, PDO::PARAM_STR);
$stmt1->execute();
$count = $stmt1->rowCount();
echo $count;

我想这会让你如愿以偿。但我只得到一个回复。结构如下
+-----------------+------------------+------+-----+---------+----------------+
| Field           | Type             | Null | Key | Default | Extra          |
+-----------------+------------------+------+-----+---------+----------------+
| id              | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| liked_post_id   | varchar(255)     | YES  |     | NULL    |                |
| liked_post_user | varchar(255)     | YES  |     | NULL    |                |
| liked_post_ip   | varchar(255)     | YES  |     | NULL    |                |
+-----------------+------------------+------+-----+---------+----------------+

快速询问
+----+---------------+-----------------+---------------+
| id | liked_post_id | liked_post_user | liked_post_ip |
+----+---------------+-----------------+---------------+
| 77 | 68            | alex            | 127.0.0.1     |
| 78 | 68            | andrew          | 127.0.0.1     |
+----+---------------+-----------------+---------------+

所以我不希望得到1,而是希望得到2,因为有两个用户喜欢它。有什么想法吗?我也没有任何错误。

最佳答案

执行以下操作:

$stmt1 = $con->prepare("SELECT COUNT(*) AS count FROM likes WHERE liked_post_id = :id;");
$stmt1->bindValue(':id', $id, PDO::PARAM_STR);
$stmt1->execute();
$row = $stmt1->fetch(PDO::FETCH_ASSOC);
echo $row['count'];

查询返回一行,该行的内容是表中匹配行的计数。您需要获取行来读取该值。

关于php - 仅返回一条记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24481358/

10-11 08:33