Blue
Blue

Reputation: 57

Count function not counting all rows in db table

I am trying to count the number of rows in a table, using the count function. I currently have 5 rows in the table in question. However the count function is only counting 1 row. Why is this? Any suggestions.

 $count_pcode = mysqli_query($dbc, "SELECT COUNT(*)FROM Delivery_Pcode");
 $count_row =mysqli_num_rows($count_pcode);
 printf("%d results.\n",$count_row);
 mysqli_free_result($count_pcode );
 mysqli_close($dbc);

Upvotes: 1

Views: 80

Answers (3)

Scott Swezey
Scott Swezey

Reputation: 2127

This code should count the number of rows, and do so efficiently.

$result = mysql_query($dbc, "SELECT COUNT(*)FROM Delivery_Pcode");

// Verify it worked
if (!$result) echo mysql_error();

$row = mysql_fetch_row($result);

// Should show you an integer result.
print_r($row);

mysqli_free_result($result);
mysqli_close($dbc);

Upvotes: 2

GROVER.
GROVER.

Reputation: 4378

Try the following:

$count_pcode = mysqli_query($dbc, "SELECT * FROM Delivery_Pcode");
$count_row = mysqli_num_rows($count_pcode);

Upvotes: 1

A. Ilazi
A. Ilazi

Reputation: 341

Just change the first line to:

$count_pcode = mysqli_query($dbc, "SELECT * FROM Delivery_Pcode");

The next line of code will then tell you how many rows your query return

$count_row =mysqli_num_rows($count_pcode);

Upvotes: 2

Related Questions