if($sql = $db->query("Count (*) FROM post_items")){
echo mysqli_num_rows($sql);
}
what's wrong with my code? is this the correct way to echo the total of row in a table?
Your query should be
select count(*) FROM post_items
but echoing
mysqli_num_rows($sql);
will always give 1 as the ans, because count function returns only one row.
Rather fetch the details and show the count
$row = $sql->fetch_row();
echo $row[0];
No it is not; you will always get a return value of 1.
Why? Because you are in essence double-counting. The query executes a COUNT aggregate on the table returning a single row with the counted number. mysqli_num_rows is then counting the number of rows in this result set - the single row - and returns 1.
Try, the following line instead, which should fetch the first (only) column returned of the first (only) row in the result set.
echo $sql->fetch_row()[0]
Note you're also missing a SELECT keyword in your SQL statement.
It should be
if($sql = $db->query("select count(*) FROM post_items")){
echo mysqli_num_rows($sql);
}
Related
So I have this query like this
$sql2 = "SELECT count(*) FROM comments WHERE YourUsername = '$MyUsername' AND PostId = '$PostId'";
if ($result2=mysqli_query($conn,$sql2))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result2);
echo $rowcount;
}
and I have 2 rows in my database which meet the clause requirements but for some reason it keeps outputting 1 as the result. How do I make it display the actual count and not just 1 when in reality the count is 2 and so on for future rows.
You're SELECTing the count of the rows in your first line, so when the query is run, it's returning the row count into $result2. You don't need to use mysqli_num_rows.
Foul
Before I was using this code to count rows
echo $db_handle->numRows("SELECT * FROM orders WHERE DATE(reattemptdate) = CURDATE()");
than i found it effects on performance, than i tried to use below code, but it not giving correct number of rows, what wrong i done in below code ?
$sqldelivery = "SELECT COUNT(*) as count FROM orders WHERE DATE(reattemptdate) = CURDATE()";
$resultdeliverys = $db_handle->runSelectQuery($sqldelivery);
$numrowsresultdelivery =count($resultdeliverys);
echo $numrowsresultdelivery;
Database connection code :
function numRows($query) {
$result = mysqli_query($this->conn,$query);
$rowcount = mysqli_num_rows($result);
return $rowcount;
}
In your second code, the query will always return 1 row - a row with the column count being the number of rows, so...
$numrowsresultdelivery =count($resultdeliverys);
Will probably always be 1, you need something like...
$numrowsresultdelivery =$resultdeliverys[0]['count'];
to extract the count field from the first row of the result.
(Note I don't know if this is the right notation, but it's the principle of needing a field from the result rather than the number of results.)
You must already have the count number via $resultdeliverys->count.
$sqldelivery = "SELECT COUNT(*) as count FROM orders WHERE DATE(reattemptdate) = CURDATE()";
$resultdeliverys = $db_handle->runSelectQuery($sqldelivery);
// Try this to know if it's returning array or object
var_dump($resultdeliverys);
we have a table usersandgroups and it has four rows.I am trying to fetch the row count as per group id but it is returning 1 for every group id but it has many rows......one more point when i remove where clause condition even then it is returning 1...that means it is always returning 1.
<?php
require_once("db_connect.php");
$gp_id = $_POST["GP_ID"];
$query = "SELECT COUNT(*) FROM USER_ANDGROUPS WHERE GP_ID = '$gp_id';";
$res = mysqli_query($con,$query);
if(mysqli_num_rows($res)>0)
{
$row_cnt = $res->num_rows;
echo $row_cnt ;
}
else
{
echo "false";
}
?>
I am trying to fetch the row count as per group id but it is returning 1 for every group id but it has many rows......one more point when i remove where clause condition even then it is returning 1...that means it is always returning 1.
SELECT COUNT(*) FROM will always return a single row which has the complete record number in itself
Change SELECT COUNT(*) FROM to SELECT * FROM and you are good to go
Note: Your code is wide open for SQL INJECTION. so use prepared statements to prevent from that.
mysqli::prepare
I am having trouble with counting the rows in a table. I want to count how many rows there are where question_id is the same as $id. However, when I echo $total_records the result is just 1.
I'm expecting 21. I'm completely sure that the variables and database information is correct. Could anyone explain what I'm doing wrong?
$sqlwow="SELECT COUNT(a_id)
FROM forum_answer
WHERE question_id='$id'
ORDER BY a_id DESC";
$rsresult = $mysqli->query($sqlwow);
$row=$rsresult->fetch_assoc();
$total_records = count($row);
echo $total_records;
$sqlwow="SELECT COUNT(*)
FROM forum_answer
WHERE question_id='$id'
ORDER BY a_id DESC";
$rsresult = $mysqli->query($sqlwow);
$row = $rsresult->fetch_row();
$total_records = $row[0];
You shouldn't fetch as an associative result when all you have is one column. PDO actually has a shortcut for this using just fetchColumn to get the first column. MySQLi doesn't seem to have this shortcut so you can fetch the whole row as a numeric array then return the first result in that array (the first column) which will be the count.
You should also just use count(*) or count(1). Using a column name is slower as it doesn't check how many rows are returned, but how many non-null values of that column are returned. In most cases, this is the same, especially if that column is the primary key. Thanks for the clarification by #fthiella.
count($row) will always be 1. You need to alias your count in the SQL statement:
$sqlwow="SELECT COUNT(a_id) as num_records
FROM forum_answer
WHERE question_id='$id'
ORDER BY a_id DESC";
Then echo it as:
$total_records = $row['num_records'];
echo $total_records;
I'm wondering why my MySQL COUNT(*) query always results in ->num_rows to be equal 1.
$result = $db->query("SELECT COUNT( * ) FROM u11_users");
print $result->num_rows; // prints 1
Whereas fetching "real data" from the database works fine.
$result = $db->query("SELECT * FROM u11_users");
print $result->num_rows; // prints the correct number of elements in the table
What could be the reason for this?
Because Count(*) returns just one line with the number of rows.
Example:
Using Count(*) the result's something like the following.
array('COUNT(*)' => 20);
echo $result['COUNT(*)']; // 20
Reference
It should return one row*. To get the count you need to:
$result = $db->query("SELECT COUNT(*) AS C FROM u11_users");
$row = $result->fetch_assoc();
print $row["C"];
* since you are using an aggregate function and not using GROUP BY
that's why COUNT exists, it always returns one row with number of selected rows
http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html
Count() is an aggregate function which means it returns just one row that contains the actual answer. You'd see the same type of thing if you used a function like max(id); if the maximum value in a column was 142, then you wouldn't expect to see 142 records but rather a single record with the value 142. Likewise, if the number of rows is 400 and you ask for the count(*), you will not get 400 rows but rather a single row with the answer: 400.
So, to get the count, you'd run your first query, and just access the value in the first (and only) row.
By the way, you should go with this count(*) approach rather than querying for all the data and taking $result->num_rows; because querying for all rows will take far longer since you're pulling back a bunch of data you do not need.