How to count the number of rows in mysql database tables with php?
if have in database 5 rows, they number show like this(bold):
all columns is: 5
1 row1 2 row2 3 row3 4 row4 5
row5
Just use this SQL query:
SELECT COUNT(*) FROM table_name
If you want to know how many rows were returned by another query, you can use the PHP mysql_num_rows function. (You could also just increment a counter for each row you process, but this function is handy if you need to know the number of records prior to enumerating the results.)
How do we count them back together? LIKE: 1 2 3 4 5. i not want use of id in column database
select list_of_fields,#rn:=#rn+1 as row_num
from table,(select #rn:=0) as r order by id
You can use this
$result = $this->db->get(<table_name>);
$num_rows = $result->num_rows();
$num_rows would be the total rows in table_name
Then you can just do this
echo 'The number of rows in table_name is '.$num_rows;
This query should do the trick for you:
SELECT COUNT(*) as count FROM `table`
The result set would be
[count]
-------
5
Assuming you have 5 rows.
In order to manually count each row and display its index (without using ID), I would do something like
$counter = 1;
$stmt = $db->prepare('SELECT `field` FROM `table`');
$stmt->execute();
while($row = $stmt->fetch()) {
echo "<b>{$counter}:</b> {$row['field']}";
$counter++;
}
Related
$query = "SELECT sum(DAILYTIME) FROM $mytable LIMIT 5";
that statement selects and sums limited to 5 rows, but how to do it based on certain row. For example I want to use row #10 as a starting point and sum 5 rows below that one ( 5 rows including that same row ).
how would the statement need to be, because if i do WHERE DAY = '$day' LIMIT 5
it will not sum all the 5 rows, just 1
By adding a where clause with the >= operator, you will also select all following rows. Use the following query:
$query = "SELECT sum(DAILYTIME) FROM $mytable WHERE DAY >= '$day' LIMIT 5";
This will give you the sum of the 10th through 14th "rows"; however, since there is no ORDER BY, there is no guarantee which rows of the table the result rows of the subquery would be.
SELECT sum(DAILYTIME)
FROM (
SELECT DAILYTIME
FROM $mytable
LIMIT 9, 5
) AS `rows10to15`
;
I have a column result in my table and want to know how many rows are affected where result contains a specific number like '3' in the last 25 rows before a specific id.
Eg: Want to know how many rows have a result = 3 of the 25 rows before the id "500".
What is the most efficiƫnt way to reach this in Php and MySQL.
You can do that completely with SQL:
SELECT COUNT(x.result)
FROM (
SELECT *
FROM table
WHERE id < 500
ORDER BY id DESC
LIMIT 25
) x
WHERE x.result = 3
First you find all entries that are relevant to your query by specifying the search criteria. Then you limit this search to 25 items and sort it in reverse order. That should yield the 25 last elements. Finally you just do your COUNT() for items with a result value of 3.
SELECT COUNT(result)
FROM table
WHERE id < '$specificID'
AND result = 3
ORDER BY id DESC
LIMIT 25
Try this
SELECT count(*) FROM `your_table`
WHERE result LIKE '%3%'
AND id < 500
ORDER BY id ASC
LIMIT 25;
does anyone know how you would count the total number of times a value appears in a single column.
for instance my table looks like:
user_id | liked_id | likes
3 1 1
4 1 1
what i want to do is count the total of the likes column where liked_id matches.
So liked_id 1 has 2 likes?
can someone please show me how i might do this?
function count_likes() {
global $connection;
global $profile_id;
$query = "SELECT likes, count(*) FROM ptb_likes GROUP BY liked_id";
$count_likes_set = mysql_query($query, $connection);
confirm_query($count_likes_set);
return $count_likes_set;
}
Try this:
SELECT liked_id, SUM(likes) FROM ptb_likes GROUP BY liked_id
you should grouped them by liked_id
SELECT liked_id, count(*) totalLikes
FROM ptb_likes
GROUP BY liked_id
if you just want the number of rows with like_id of 1,
SELECT liked_id, COUNT(likes) FROM ptb_likes WHERE liked_id = 1;
If you want the the frequency of each like_id:
SELECT liked_id, COUNT(likes) FROM ptb_likes GROUP BY liked_id;
As a note: don't use SUM as that adds all the values of like_id, and if you have a like_id greater than 1, your count will be wrong. Use count instead.
If I have 5 rows in my mysql database with ids of 2, 4, 5, 6 and 8 and I echo out id 5 how do I then echo out the row number which of course would be row 3? I get row 3 by counting all the rows from the start of the database to the row I am after.
Try to use
SELECT #row_num:=#row_num+1 as row_number, id from table inner join (select #row_num:=0) as temp
$pdo = new PDO(....);
$result = $pdo->query($sql)->fetchAll();
foreach($result as $row) {
print_r($row);
}
The correct solution would be to add some auto_increment key to your table, then each row would have assigned a sequential number. Otherwise you are not guaranteed to get the same number for each id because SQL query without ORDER BY clause is not guaranteed to have any order (i.e. ids may be returned in random order).
Otherwise, the solution given by Andrej L is the correct one.
Couldn't you just count the number of rows before this in the sort order you're using?
COUNT(*) FROM table WHERE id < ?
In this case only rows with id 2 and 3 would count, so the result is 2. If you're using a 1-based index, add one to that and you have your answer.
I have a mysql table. It has auto increment on the id. but I regularly delete rows so the numbers are all over the place. I need to get the last n rows out, but because of deletions, the common way of using the max of the autoincremented id column doesn't work well...
1 - Is their another way to get the bottom 50?
2 - Is their a way to get rows by actual row number? so if I have 4 rows labelled 1,2,3,4 delete row 2 then it will become 1,2,3 rather than 1,3,4?
SELECT ... ORDER BY id DESC LIMIT 50
SELECT *
FROM TABLE
ORDER BY id DESC
LIMIT 50
EDIT
To pick the last 50, but sort by id ASC
SELECT X.*
FROM ( SELECT *
FROM TABLE
ORDER BY id DESC
LIMIT 50
) X
ORDER BY X.id
1 - First get total row count like
SELECT COUNT(*) AS c FROM ...
then use
SELECT ..... LIMIT [start],[count]
2 - One idea is to use view , or procedure, but this is much more harder and may be used when there is no other way to avoid this
1 - Is their another way to get the bottom 50?
SELECT * FROM table_name ORDER BY record_id DESC LIMIT 50
2 - Is their a way to get rows by actual row number? so if I have 4 rows labelled 1,2,3,4 delete row 2 then it will become 1,2,3 rather than 1,3,4?
SELECT * FROM table_name
1 - Yes but it is ugly afaik, you do a
SELECT whateveryouwant FROM table ORDER BY yourprimarykey DESC LIMIT 50
the you fetch the rows into an array and reverse the array, in php :
$r = mysql_query('SELECT * FROM table ORDER BY primarykey DESC LIMIT 50');
$set = array();
while($row = mysql_fetch_assoc($r)) $set = $row;
$set = array_reverse($set);
foreach($set as $row) {
// display row ...
}
2 - You'll have to manage your primary key by yourself, its a bit risky ...