Php I';我不确定我错在哪里。只有5行代码

Php I';我不确定我错在哪里。只有5行代码,php,mysql,count,Php,Mysql,Count,我需要做的就是计算表中的所有字段,其中postid=38 <? $consulta2 = mysql_query("select count(*) from $tabla_db4 where postid='38';"); $result2 = mysql_num_rows($consulta2); echo (string) $result2; ?> 但我的代码总是在屏幕上打印“1”,不管我写的是什么数字postid=38 <?

我需要做的就是计算表中的所有字段,其中
postid=38

<? 
    $consulta2 = mysql_query("select count(*) from $tabla_db4 where postid='38';");    
    $result2 = mysql_num_rows($consulta2); 
    echo (string) $result2; 
?>
但我的代码总是在屏幕上打印“
1
”,不管我写的是什么数字
postid=38

<? 
    $consulta2 = mysql_query("select count(*) from $tabla_db4 where postid='38';");    
    $result2 = mysql_num_rows($consulta2); 
    echo (string) $result2; 
?>

您可以执行以下任一操作:

  <? 
   $consulta2 = mysql_query("select count(*) as count from $tabla_db4 where postid='38';");    
   $result2 = mysql_fetch_array($consulta2); 
   echo $result2['count']; 
 ?>
解决了!请关上

$sql = mysql_query(
       "SELECT * FROM `comentarios` WHERE `postid` =
                                         $registro[id] ORDER BY `postid` DESC",$conexion_db);
$cuenta = mysql_num_rows($sql);
echo $cuenta;

如果您想知道有多少行,可以尝试以下方法:

<?php
    $query = "select count(*) as amount from $tabla_db4 where postid='38';";

    $result = mysql_query($query);

    if(!$result) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $query;
        die($message);
    }

    while($row = mysql_fetch_assoc($result)) {
        echo $row['amount'];
    }

    mysql_free_result($result);
?>


另外,您确定posted是一个字符串,因为您将它放在引号之间。看起来它可能是一个整数。注意数据类型。

COUNT总是返回一行;读取那一行,行数据中的值就是计数值。。。。请注意,有时将别名与聚合函数一起使用会有所帮助,从而更容易从该行访问值,因为您选择的是行数
mysql\u num\u rows
,而不是读取查询结果。它将返回一行和一个字段,其中包含countPlease。已在PHP7中删除。了解使用PDO的语句,并考虑使用PDO。