打印出 COUNT - SQL / PHP

发布于 2024-11-30 06:11:05 字数 308 浏览 0 评论 0原文

我有以下代码:

$topics= mysql_query("SELECT COUNT(*) FROM forum_topics WHERE forum_id=".$h['forum_id'].""); 
                print $topics; //This prints out 1, but should be 14?

如您所见,我从表中选择 COUNT。该表包含 14 行。我怎样才能打印出来?现在,当我打印 $topics 时,它只是显示 Resource Id #18。

I have this code:

$topics= mysql_query("SELECT COUNT(*) FROM forum_topics WHERE forum_id=".$h['forum_id'].""); 
                print $topics; //This prints out 1, but should be 14?

As you can see, I select COUNT from my table. That table contains 14 rows. How can I print this out? As now, when I print out $topics, it just says Resource Id #18.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清醇 2024-12-07 06:11:05

你需要做:

$topics = mysql_query("SELECT COUNT(*) FROM forum_topics WHERE forum_id=".$h['forum_id'].""); 
$result = mysql_fetch_assoc($topics);
print $result['COUNT(*)'];

You need to do:

$topics = mysql_query("SELECT COUNT(*) FROM forum_topics WHERE forum_id=".$h['forum_id'].""); 
$result = mysql_fetch_assoc($topics);
print $result['COUNT(*)'];
半夏半凉 2024-12-07 06:11:05

那么你需要类似的东西

if ($row = mysql_fetch_row($topics))
{
   echo $row[0];
}

Then you need something like

if ($row = mysql_fetch_row($topics))
{
   echo $row[0];
}
七婞 2024-12-07 06:11:05
$topics= mysql_query("SELECT COUNT(*) as Count FROM forum_topics WHERE forum_id=".$h['forum_id']."");

$topics 不会打印 14。该查询在成功时返回资源ID,在错误时返回false

来源:链接

如果你想获取计数,你可以这样做,

$rows = mysql_fetch_array($topics) //You can use this since it's only one record
{
     echo $rows['Count'];
}

如果你必须获取多条记录,你可以使用

while($rows = mysql_fetch_array($topics))
{
     echo $rows['Count']."</br>";
}
$topics= mysql_query("SELECT COUNT(*) as Count FROM forum_topics WHERE forum_id=".$h['forum_id']."");

$topics will not print 14. The query returns Resource ID on success and false on error.

Source: Link.

If you want to get the Count you can do,

$rows = mysql_fetch_array($topics) //You can use this since it's only one record
{
     echo $rows['Count'];
}

If you have to get multiple records you can use

while($rows = mysql_fetch_array($topics))
{
     echo $rows['Count']."</br>";
}
飘过的浮云 2024-12-07 06:11:05
$topics= mysql_query("SELECT COUNT(*) as count FROM forum_topics WHERE forum_id=".$h['forum_id'].""); 
while($row = mysql_fetch_array($topics)){
    echo $row['count'];
}
$topics= mysql_query("SELECT COUNT(*) as count FROM forum_topics WHERE forum_id=".$h['forum_id'].""); 
while($row = mysql_fetch_array($topics)){
    echo $row['count'];
}
月棠 2024-12-07 06:11:05

这是因为 $topics 是一种资源,而不是结果集。您需要使用 mysql_fetch_assoc、mysql_fetch_array 获取结果集(数组),或者在这种情况下可以使用 mysql_fetch_row。

http://us.php.net/manual/en/ref.mysql.php

That is because $topics is a resource, not a result set. You need to get a result set(array) using mysql_fetch_assoc, mysql_fetch_array, or you can use mysql_fetch_row in this case.

http://us.php.net/manual/en/ref.mysql.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文