mysql_num_rows always returns 1

前端 未结 4 387
小蘑菇
小蘑菇 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条回答
  •  难免孤独
    2020-11-30 16:09

    mysql_num_rows returns the number of selected rows and not the fields of a certain row. Use mysql_fetch_row to fetch the row you have selected with your query:

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

    You could also use mysql_result to fetch a row and get a certain field:

    $vote_total = mysql_result($res, 0, 0);
    

    This fetches the first row (zero based) and returns the first field (zero based).

提交回复
热议问题