简体   繁体   English

如何在我的情况下正确使用SQL计数

[英]How to correctly use SQL count in my situation

On my website, I have implemented a reply system, or a "mention" system and now I want to let the user know when they have a new mention without having to go to the mentions page and manually checking. 在我的网站上,我已经实现了答复系统或“提及”系统,现在我想让用户知道何时有新的提及,而不必转到提及页面并手动检查。 On my website, I have a sidebar with navigation links, I'd like to have the mentions link say something like "Mentions(2)" where 2 is however many new mentions you have. 在我的网站上,我有一个带有导航链接的侧边栏,我想让提及链接说出类似“ Mentions(2)”,其中2是您有很多新提及。

So the only problem I am having is returning the count of the number of rows that this SQL query returns. 因此,我唯一的问题是返回此SQL查询返回的行数的计数。

Using this I get "Mentions(Array)", but how would I retrieve the number of how many rows is being selected? 使用此方法,我将获得“ Mentions(Array)”,但是如何检索选择的行数呢?

$getMentions = $db->prepare("SELECT COUNT(seen) AS readMentions FROM mentions WHERE mentioned = :username AND seen = 0");

$getMentions->execute(array(':username' => $_SESSION['USER']));

$mentions = $getMentions->fetch();

echo "<li><a href='../mentions.php'>Mentions (".$mentions.")</a></li>";

fetch returns an array indexed, by default, by both column name and a zero based index with the results of the row. 默认情况下, fetch返回一个数组,该数组同时由列名和行结果从零开始的索引。 So instead of echo ing $mentions , you should be echo ing $mentions['readMentions'] or $mentions[0] . 因此,您应该echo $mentions['readMentions']$mentions[0]而不是echo $mentions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM