Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
打印输出计数-SQL/PHP_Php_Sql_Printing_Count - Fatal编程技术网

打印输出计数-SQL/PHP

打印输出计数-SQL/PHP,php,sql,printing,count,Php,Sql,Printing,Count,我有以下代码: $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时,它只显示资源Id 18。那么您需要 if ($

我有以下代码:

$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时,它只显示资源Id 18。

那么您需要

if ($row = mysql_fetch_row($topics))
{
   echo $row[0];
}
您需要执行以下操作:

$topics = mysql_query("SELECT COUNT(*) FROM forum_topics WHERE forum_id=".$h['forum_id'].""); 
$result = mysql_fetch_assoc($topics);
print $result['COUNT(*)'];
$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\u fetch\u assoc、mysql\u fetch\u数组获取结果集数组,或者在本例中可以使用mysql\u fetch\u行


你读过一些关于使用mysql PHP函数的教程/文档吗?是的,我读过COUNT*函数,尽管它是使用WHILE循环完成的。我希望可以避免这种情况。返回的结果资源应该传递给mysql_fetch_数组和其他处理结果表的函数,以访问返回的数据。-mysql\u查询文档。如果不需要循环,则不需要循环,但需要使用这些函数。然后,查询出现问题。表格中forum_id列下是否有$h['forum_id']值的条目?对不起!我的错。我的陈述有错误。谢谢你的回答!对于SELECT查询,mysql\u查询返回一个资源ID,失败时返回false。您是对的,SELECT返回资源ID,INSERT、UPDATE、DELETE、DROP等返回true。
$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>";
}