mysql_num_rows always returns 1

前端 未结 4 378
小蘑菇
小蘑菇 2020-11-30 15:45

The result is always 1:

$sql = \'SELECT COUNT(Vote) FROM \' . $table;
$res = mysql_query($sql, $conn);
$vote_total = mysql_num_rows($res);

4条回答
  •  萌比男神i
    2020-11-30 15:58

    The tricky part is that even when the sql query contains a COUNT() statement, it is still a query result like any other. MySQL will return a row containg a single column with the number of rows that would have been returned, should you have issued a reglar query. mysql_num_rows() on the other hand, just counts the number of rows in the result of the query that was actually executed. In this case it is always a single row.

    What you want is:

    $sql = 'SELECT COUNT(Vote) FROM ' . $table;
    $res = mysql_query($sql, $conn);
    $data = mysql_fetch_row($res);
    $vote_total = $data[0];
    

提交回复
热议问题