Lee Lee - 6 months ago 9
PHP Question

PHP MySQL not returning the correct count

I have verified my query using phpMyAdmin. The count is 130. But my code is giving me a count of 1.

$query2 = "SELECT COUNT(*) FROM webvulns";
$result2 = mysqli_query($connection, $query2);
confirm_query($result2);
$rowcount=mysqli_num_rows($result2);
echo $rowcount;
// Free result set.
mysqli_free_result($result2);

Answer

mysqli_num_rows returns a count of rows. You only have 1 row. That row will have 130 in it. Fetch the result.

$query2 = "SELECT COUNT(*) as da_count FROM webvulns";
$result2 = mysqli_query($connection, $query2);
confirm_query($result2);
$row = mysqli_fetch_assoc($result2);
$rowcount=$row['da_count'];
echo $rowcount;
// Free result set.
mysqli_free_result($result2);
Comments