How many rows in my table?
Moderator: General Moderators
-
JavaScript
- Forum Newbie
- Posts: 7
- Joined: Tue Jan 28, 2003 8:54 pm
How many rows in my table?
How can i count the number of rows in my table and display them to the browser?
And if i need to count the number of rows in multiple tables (e.g. 7 tables), how can i do that?
My table contains a field of ID (auto increment), but since i sometimes delete certain rows, this ID value doesn't show the exact rows in the table
Thank for you help!
And if i need to count the number of rows in multiple tables (e.g. 7 tables), how can i do that?
My table contains a field of ID (auto increment), but since i sometimes delete certain rows, this ID value doesn't show the exact rows in the table
Thank for you help!
Assuming a MySQL database:
You would do this for each table you wish to count the rows of.
Code: Select all
$sql = "select count(*) from table_name" ;
$result = mysql_query($sql) ;
$row = mysql_fetch_array($result) ;
echo "table_name has " . $row[0] . "rows." ;-
bionicdonkey
- Forum Contributor
- Posts: 132
- Joined: Fri Jan 31, 2003 2:28 am
- Location: Sydney, Australia
- Contact:
you could also do...
Code: Select all
<?php
$query = mysql_query("SELECT * FROM table");
$num_rows = mysql_num_rows($query);
echo $num_rows;
?>- AVATAr
- Forum Regular
- Posts: 524
- Joined: Tue Jul 16, 2002 4:19 pm
- Location: Uruguay -- Montevideo
- Contact:
Use this only if you are going to display the result of the query... if you only need the number of rows usebionicdonkey wrote:you could also do...Code: Select all
<?php $query = mysql_query("SELECT * FROM table"); $num_rows = mysql_num_rows($query); echo $num_rows; ?>
Code: Select all
SELECT count(row) FROM table-
JavaScript
- Forum Newbie
- Posts: 7
- Joined: Tue Jan 28, 2003 8:54 pm
So...
So... what does the exact code look like?
My table structure is here
I just need to count how many rows in that table, then print out something like
We have $num_of_rows tutorials
One more question (This is another topic in this forum, but since i can't figure out the way, i ask it in this real thread)
If i want to show
The latest tutorial is link_to_id_of_the_last_row $title_of_the_last_row
How can i do that?
My table structure is here
Code: Select all
table my_tutorial (
id int not null auto_increment,
title varchar(255) not null,
author varchar(55) not null,
description text not null,
date int(14) not null,
tutorial mediumtext not null,
view int not null,
primary key (id),
unique id (id)
)We have $num_of_rows tutorials
One more question (This is another topic in this forum, but since i can't figure out the way, i ask it in this real thread)
If i want to show
The latest tutorial is link_to_id_of_the_last_row $title_of_the_last_row
How can i do that?
- AVATAr
- Forum Regular
- Posts: 524
- Joined: Tue Jul 16, 2002 4:19 pm
- Location: Uruguay -- Montevideo
- Contact:
the solution
The solution is in previous posts!!!!
For the second question you have to change the SQL statement and use MAX(id).
Code: Select all
<?php
$sql = "select count(id) from my_tutorial" ;
$result = mysql_query($sql) ;
$row = mysql_fetch_array($result) ;
echo "we have " . $row[0] . " tutorials." ;
?>-
JavaScript
- Forum Newbie
- Posts: 7
- Joined: Tue Jan 28, 2003 8:54 pm
Yeah...
The code surely helped
But for now, another problem appears - i can't update column view
but it isn't add 1 to the existing number
But for now, another problem appears - i can't update column view
Code: Select all
$result = mysql_query("select * from my_tutorial where id=$id");
while($row = mysql_fetch_array($result)) {
$v = $rowїview];
$vplus = $v + 1;
$view_update = mysql_query("update my_tutorial where id=$id set view = '$vplus'");
}- AVATAr
- Forum Regular
- Posts: 524
- Joined: Tue Jul 16, 2002 4:19 pm
- Location: Uruguay -- Montevideo
- Contact:
Re: Yeah...
Check:
Code: Select all
<?php
$v = $row["view"];
?>Your code contains error in the SQL statement
Use that! It will work
Code: Select all
$view_update = mysql_query("update my_tutorial їb]SETї/b] view = view + 1 where id=$id");