How can I count all rows that there are in a table - php

I searched in google and I found the following command:
SELECT COUNT(*) FROM table_name
And I used the above code as follows to count all rows that there are in a table.
$number = mysqli_query($connection,"SELECT COUNT(*) FROM table_name");
$rows = mysqli_num_rows($number);
echo $rows;
But when I see the result using the echo command, I see the result as the 1 value. While number of rows in my table is 12.

The query just returns 1 row. That row contains the count of rows in it. You need to fetch the row to get the answer, just like you do to get the results from any other query.
$result = mysqli_query($connection,"SELECT COUNT(*) AS count FROM table_name");
$row = mysqli_fetch_assoc($result);
$rows = $row['count'];
echo $rows;

Related

How to execute function only if MySQL table is empty? [duplicate]

I'm trying to count the number of rows in a table and thought that this was the correct way to do that:
$result = $db->query("SELECT COUNT(*) FROM `table`;");
$count = $result->num_rows;
But counts always returns (int)1. If I use the same query in phpMyAdmin I get the right result. It sits in a table so I tried testing $count[0] as well, but that returns NULL.
What is the right way to do this?
You have to fetch that one record, it will contain the result of Count()
$result = $db->query("SELECT COUNT(*) FROM `table`");
$row = $result->fetch_row();
echo '#: ', $row[0];
Always try to do an associative fetch, that way you can easy get what you want in multiple case result
Here's an example
$result = $mysqli->query("SELECT COUNT(*) AS cityCount FROM myCity")
$row = $result->fetch_assoc();
echo $row['cityCount']." rows in table myCity.";
I find this way more readable:
$result = $mysqli->query('select count(*) as `c` from `table`');
$count = $result->fetch_object()->c;
echo "there are {$count} rows in the table";
Not that I have anything against arrays...
$result->num_rows; only returns the number of row(s) affected by a query. When you are performing a count(*) on a table it only returns one row so you can not have an other result than 1.

mysql rows count by where clause

look at the database here ---->http://www.tiikoni.com/tis/view/?id=908d940
in the subcommentid rows, value 63 is repeating maximum time itself 7 times
i want to fetch this value in numerical form means like this
maximum rows in subcommentid is 7
i hv tried this
$query=mysqli_query("SELECT * FROM table WHERE where id='63'");
echo mysqli_num_rows($query);
but
but 63 is a variable so how can a server pre decide that 63 is repeating itself maximum nos
nd look at dix ques too cant get my desire result of max(like)
use select count(*) from.....
Or
use mysql function mysql_num_rows($query_result).
Try using this query -
$query = mysqli_query("SELECT subcommentid,COUNT(*) as count FROM table GROUP BY subcommentid ORDER BY count DESC;");
$result = mysqli_fetch_array($query);
echo $result['subcommentid'].'<br/>'; //subcomment id
echo $result['count']; // number of rows
If you want all, store in array -
$results = array();
while($row = mysqli_fetch_array($query)) {
$results[$row['subcommentid']] = $row['count'];
}
echo "<pre>";print_r($results);exit;
$query = "SELECT * FROM comments WHERE id=`7`";
$result = mysqli_query($sql, $query);
$rows = mysqli_num_rows($result);
Use mysqli_num_rows function in php to count the no.of rows. Your code should look like this
$query=mysqli_query("SELECT * FROM table WHERE where id='63'");
echo mysqli_num_rows($query); // will echo no.of rows having id 63
Hope this helps you

Row count no error, but wrong result

$rowcount = 'SELECT COUNT(1) FROM (select * from isk.edi_site where postal_code = 123456)';
$stmt= oci_parse($conn, $rowcount);
oci_execute($stmt);
$num_row = oci_fetch_assoc($stmt);
$num = count($num_row, COUNT_RECURSIVE);
echo $num;
It counts and return the number "1". When I use the same SQL query in Oracle SQL developer, it echos out 4000+ count. Where would my mistake be? The column count works well..
A COUNT() query only returns a single row so you need to grab it from the oci_fetch_assoc() call. What you're doing is counting the rows in the result set which will always be 1.
$row = oci_fetch_assoc($stmt);
echo $row['COUNT(1)'];
Or give the count an alias:
SELECT COUNT(1) mycount FROM ...
$row = oci_fetch_assoc($stmt);
echo $row['MYCOUNT'];

my mysql query is resulting in 1 for number of rows regardless if value is there or not

here is my mysql statement:
$result = mysql_query("select count(*) from usersecurity where
email='".$_SESSION['username']."'");
echo mysql_num_rows($result);
Problem is, from my understanding, its always returning the value 1, even if the username doesn't exist in the table...why is that?
Because SELECT COUNT(*) ... with return a row with a single value containing zero.
COUNT(*) will always return a row, even if the result is zero. You need to examine the result of that column:
SELECT COUNT(*) AS count FROM t1 WHERE email = "SOME NON EXISTENT EMAIL";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
echo $row['count']; // 0
An alternative would be to select the count as the number of rows:
SELECT TRUE FROM t1 WHERE email = "SOME NON EXISTENT EMAIL";
$result = mysql_query($query);
echo mysql_num_rows($result); // 0
A COUNT(*) query always returns a single row. If you want the $ of rows then do one of the following.
$result = mysql_query("select count(*) as ROWS from usersecurity where
email='".$_SESSION['username']."'");
$rows = mysql_fetch_assoc($result);
echo $rows['ROWS'];
or
$result = mysql_query("select * as ROWS from usersecurity where
email='".$_SESSION['username']."'");
echo mysql_num_rows($result);
You will always get one row from the result query. That value will some value greater than or equal to zero. You need to inspect the actual count returned not the number of rows returned.
Your SELECT query is returning the following:
select count(*)...
----------------------
0
Thus, mysql_num_rows($result) is appropriately returning 1 as a result. If you're looking to get the value of your count, then you need to take an approach like the following:
$result = mysql_query("select count(*) from usersecurity where
email='".$_SESSION['username']."'");
$row = mysql_fetch_array($result);
echo $row[0];

MySQLi count(*) always returns 1

I'm trying to count the number of rows in a table and thought that this was the correct way to do that:
$result = $db->query("SELECT COUNT(*) FROM `table`;");
$count = $result->num_rows;
But counts always returns (int)1. If I use the same query in phpMyAdmin I get the right result. It sits in a table so I tried testing $count[0] as well, but that returns NULL.
What is the right way to do this?
You have to fetch that one record, it will contain the result of Count()
$result = $db->query("SELECT COUNT(*) FROM `table`");
$row = $result->fetch_row();
echo '#: ', $row[0];
Always try to do an associative fetch, that way you can easy get what you want in multiple case result
Here's an example
$result = $mysqli->query("SELECT COUNT(*) AS cityCount FROM myCity")
$row = $result->fetch_assoc();
echo $row['cityCount']." rows in table myCity.";
I find this way more readable:
$result = $mysqli->query('select count(*) as `c` from `table`');
$count = $result->fetch_object()->c;
echo "there are {$count} rows in the table";
Not that I have anything against arrays...
$result->num_rows; only returns the number of row(s) affected by a query. When you are performing a count(*) on a table it only returns one row so you can not have an other result than 1.

Categories