Reputation: 159
so i have this
$countsql = <<<SQL
SELECT COUNT(*)
FROM `deathnote`
SQL;
if ($stmt = mysqli_prepare($db, $countsql)) {
/* execute query */
mysqli_stmt_execute($stmt);
/* store result */
mysqli_stmt_store_result($stmt);
printf("Number of rows: %d.\n", mysqli_stmt_num_rows($stmt));
}
The output of this is 1. Now, when i go into PHP admin on my server and run the exact same query i receive the expected outcome of 12.
Can anybody see where i went wrong, or suggest what to do? Thanks guys!
Upvotes: 0
Views: 548
Reputation: 116
you are taking sql count function very wrong here basically its an aggregate function which returns only 1 as you used printf("Number of rows: %d.\n", mysqli_stmt_num_rows($stmt)); if you want your desired result which you say is 12 then you will can use mysqli_result, mysqli_fetch_array i-e
Upvotes: 0
Reputation: 8179
Either use mysqli_stmt_num_rows
OR use COUNT(*)
don't mix both.
With mysqli_stmt_num_rows
Query should be
SELECT *
FROM `deathnote`;
OR
With COUNT(*)
Recommend to use this way for better performance and fast execution of code
$res = mysqli_query("SELECT COUNT(*) FROM deathnote");
$row = mysqli_fetch_array($res)
echo $row[0];
Upvotes: 0
Reputation: 157870
$res = mysqli_query($db, "SELECT COUNT(*) FROM deathnote");
$row = mysqli_fetch_row($res)
echo "Number of rows: $row[0]\n";
Upvotes: 1
Reputation: 44833
You want the value of COUNT(*), not the number of rows -- the number of rows will always be one: a single row, with one column, containing the value of COUNT(*).
Upvotes: 0