473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How many rows in a table

I am sure there must be an easy way to determine the number of
rows in a table, but I can't find it.

I appreciate the courtesy and patience ng members have shown this
mysql novice.

bill
Dec 30 '06 #1
9 1876
On Sat, 30 Dec 2006 17:04:58 -0500, bill <no****@spamcop.netwrote:
>I am sure there must be an easy way to determine the number of
rows in a table, but I can't find it.

I appreciate the courtesy and patience ng members have shown this
mysql novice.
select count(*) from your_table

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Dec 30 '06 #2
Andy Hassall wrote:
On Sat, 30 Dec 2006 17:04:58 -0500, bill <no****@spamcop.netwrote:
>I am sure there must be an easy way to determine the number of
rows in a table, but I can't find it.

I appreciate the courtesy and patience ng members have shown this
mysql novice.

select count(*) from your_table
$total_rows = mysql_query ("SELECT COUNT(*) FROM Kennel");

gives me a resource, not the count.

Fine, so I use:

$total_rows = mysql_query ("SELECT COUNT(*) FROM Kennel");
echo "Rows in table: " . mysql_num_rows ($total_rows) . "<br />";

but that returns 1, and there are 4 rows in the table.
However,

$sql = "Select id, kennel_name, location, real_names from Kennel
ORDER BY REPLACE(kennel_name,'The ','') LIMIT 100";
$result = mysql_query($sql, $connection) or die(mysql_error());
echo "Rows in table-2: " . mysql_num_rows ($result) . "<br />";

does give the correct answer.

what am I doing wrong with the first query ?
Dec 31 '06 #3
GT
bill wrote:
Andy Hassall wrote:
>On Sat, 30 Dec 2006 17:04:58 -0500, bill <no****@spamcop.netwrote:
>>I am sure there must be an easy way to determine the number of rows
in a table, but I can't find it.

select count(*) from your_table

$total_rows = mysql_query ("SELECT COUNT(*) FROM Kennel");

gives me a resource, not the count.
I guess it would. If you were to do it that way:

$result = mysql_query ("SELECT COUNT(*) FROM Kennel");
$array = mysql_fetch_array($result);
$rows = $array[0];

(or similar - I've not tested, but something akin to that will work)
Fine, so I use:

$total_rows = mysql_query ("SELECT COUNT(*) FROM Kennel");
echo "Rows in table: " . mysql_num_rows ($total_rows) . "<br />";

but that returns 1, and there are 4 rows in the table.
No no!
$result = mysql_query ("SELECT * FROM Kennel");
$rows = mysql_num_rows ($result);

HTH
--
GT
Dec 31 '06 #4
On Sun, 31 Dec 2006 10:32:02 -0500, bill <no****@spamcop.netwrote:
>Andy Hassall wrote:
>On Sat, 30 Dec 2006 17:04:58 -0500, bill <no****@spamcop.netwrote:
>>I am sure there must be an easy way to determine the number of
rows in a table, but I can't find it.

I appreciate the courtesy and patience ng members have shown this
mysql novice.

select count(*) from your_table

$total_rows = mysql_query ("SELECT COUNT(*) FROM Kennel");

gives me a resource, not the count.

Fine, so I use:

$total_rows = mysql_query ("SELECT COUNT(*) FROM Kennel");
echo "Rows in table: " . mysql_num_rows ($total_rows) . "<br />";

but that returns 1, and there are 4 rows in the table.
The query gives a 1 column result set with 1 row, which will have the count in
it. mysql_num_rows gives you the number of rows in a result set - therefore
it's 1 here.

If you fetched the first row with mysql_fetch_array or one of the variants of
it then you can get at the value for the first column in the row - which will
be 4.
>However,

$sql = "Select id, kennel_name, location, real_names from Kennel
ORDER BY REPLACE(kennel_name,'The ','') LIMIT 100";
$result = mysql_query($sql, $connection) or die(mysql_error());
echo "Rows in table-2: " . mysql_num_rows ($result) . "<br />";

does give the correct answer.
It doesn't really, since you have a LIMIT clause in there, so if the number of
rows in the table goes over 100 it'll always say 100. It also fetches all the
columns you named out of the database into PHP, and does some ordering.

Perhaps you've asked the wrong question for what you want; if you want the
number of rows in the table but don't need the data itself, then you use an SQL
query such as "select count(*) from t" as above to get you a result set
containing a row with the count in it.

If you have already fetched the data as a result set from an SQL statement
because you need it at the same time, but you want to know how many rows are in
the result set you've fetched (which rarely corresponds exactly to a table
anyway since you'll be doing filters and joins) then you can use mysql_num_rows
on a result set resource.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Dec 31 '06 #5
Andy Hassall wrote:
On Sun, 31 Dec 2006 10:32:02 -0500, bill <no****@spamcop.netwrote:
>Andy Hassall wrote:
>>On Sat, 30 Dec 2006 17:04:58 -0500, bill <no****@spamcop.netwrote:

I am sure there must be an easy way to determine the number of
rows in a table, but I can't find it.

I appreciate the courtesy and patience ng members have shown this
mysql novice.
select count(*) from your_table
$total_rows = mysql_query ("SELECT COUNT(*) FROM Kennel");

gives me a resource, not the count.

Fine, so I use:

$total_rows = mysql_query ("SELECT COUNT(*) FROM Kennel");
echo "Rows in table: " . mysql_num_rows ($total_rows) . "<br />";

but that returns 1, and there are 4 rows in the table.

The query gives a 1 column result set with 1 row, which will have the count in
it. mysql_num_rows gives you the number of rows in a result set - therefore
it's 1 here.

If you fetched the first row with mysql_fetch_array or one of the variants of
it then you can get at the value for the first column in the row - which will
be 4.
>However,

$sql = "Select id, kennel_name, location, real_names from Kennel
ORDER BY REPLACE(kennel_name,'The ','') LIMIT 100";
$result = mysql_query($sql, $connection) or die(mysql_error());
echo "Rows in table-2: " . mysql_num_rows ($result) . "<br />";

does give the correct answer.

It doesn't really, since you have a LIMIT clause in there, so if the number of
rows in the table goes over 100 it'll always say 100. It also fetches all the
columns you named out of the database into PHP, and does some ordering.

Perhaps you've asked the wrong question for what you want; if you want the
number of rows in the table but don't need the data itself, then you use an SQL
query such as "select count(*) from t" as above to get you a result set
containing a row with the count in it.

If you have already fetched the data as a result set from an SQL statement
because you need it at the same time, but you want to know how many rows are in
the result set you've fetched (which rarely corresponds exactly to a table
anyway since you'll be doing filters and joins) then you can use mysql_num_rows
on a result set resource.
Actually I need both. I need the total rows initially to
structure the next query. The limit 100 is a stopgap while I am
writing the rest of it.
The general idea is that for up to 100 rows I will present it as
one html table, over 100 I will divide it alphabetically to have
each segment be under 100.
Thanks
bill
Dec 31 '06 #6
Andy Hassall wrote:
On Sun, 31 Dec 2006 10:32:02 -0500, bill <no****@spamcop.netwrote:
>Andy Hassall wrote:
>>On Sat, 30 Dec 2006 17:04:58 -0500, bill <no****@spamcop.netwrote:

I am sure there must be an easy way to determine the number of
rows in a table, but I can't find it.

I appreciate the courtesy and patience ng members have shown this
mysql novice.
select count(*) from your_table
$total_rows = mysql_query ("SELECT COUNT(*) FROM Kennel");

gives me a resource, not the count.

Fine, so I use:

$total_rows = mysql_query ("SELECT COUNT(*) FROM Kennel");
echo "Rows in table: " . mysql_num_rows ($total_rows) . "<br />";

but that returns 1, and there are 4 rows in the table.

The query gives a 1 column result set with 1 row, which will have the count in
it. mysql_num_rows gives you the number of rows in a result set - therefore
it's 1 here.

If you fetched the first row with mysql_fetch_array or one of the variants of
it then you can get at the value for the first column in the row - which will
be 4.
Ah, works like a charm.
Thanks. I appreciate your assistance a lot.

bill
Dec 31 '06 #7
GT wrote:
bill wrote:
>Andy Hassall wrote:
>>On Sat, 30 Dec 2006 17:04:58 -0500, bill <no****@spamcop.netwrote:

I am sure there must be an easy way to determine the number of rows
in a table, but I can't find it.
select count(*) from your_table

$total_rows = mysql_query ("SELECT COUNT(*) FROM Kennel");

gives me a resource, not the count.

I guess it would. If you were to do it that way:

$result = mysql_query ("SELECT COUNT(*) FROM Kennel");
$array = mysql_fetch_array($result);
$rows = $array[0];
yup, worked fine.

Many thanks.

bill
Dec 31 '06 #8
GT wrote:
bill wrote:
>Andy Hassall wrote:
>>On Sat, 30 Dec 2006 17:04:58 -0500, bill <no****@spamcop.netwrote:

I am sure there must be an easy way to determine the number of rows
in a table, but I can't find it.
select count(*) from your_table

$total_rows = mysql_query ("SELECT COUNT(*) FROM Kennel");

gives me a resource, not the count.


I guess it would. If you were to do it that way:

$result = mysql_query ("SELECT COUNT(*) FROM Kennel");
$array = mysql_fetch_array($result);
$rows = $array[0];
Yes, this would be the correct way to do it.
(or similar - I've not tested, but something akin to that will work)
>Fine, so I use:
$total_rows = mysql_query ("SELECT COUNT(*) FROM Kennel");
echo "Rows in table: " . mysql_num_rows ($total_rows) . "<br />";

but that returns 1, and there are 4 rows in the table.


No no!
$result = mysql_query ("SELECT * FROM Kennel");
$rows = mysql_num_rows ($result);
Definitely not! What if the table has 10M rows? You're asking they all
be returned to the program so they can be counted.

Let MySQL do the counting. That's what COUNT(*) does.
HTH


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Dec 31 '06 #9
Jerry Stuckle wrote:
Definitely not! What if the table has 10M rows? You're asking they all
be returned to the program so they can be counted.
I guess that was just the idea how to make the thing mentioned by bill
work - not the advice for using it :) However, you are certainly right -
the right way to do that is COUNT(*) built-in.
--
Mateusz Papiernik, Maticomp Webdesign
ma**@maticomp.net, http://www.maticomp.net
"One man can make a difference" - Wilton Knight
Dec 31 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: anonieko | last post by:
This example applies to javascript, table, cells, rows > > How do you access rows and columns of a HTML table? > > > <script language="javascript"> alert('start');
6
by: jhallbox | last post by:
I am unclear how to test this object I'm sending to my function. It is a cell in a table and I simply want to know which row it is in. I am not a javascript programmer but a javascript cobbler, so...
6
by: Brendan.Collins | last post by:
Hi I have a javascript problem that has been annoying me for two days now and thought that a javascript expert might have the magic solution. I am populating a table dynamically from the...
0
by: Luis Esteban Valencia | last post by:
Hello I wrote a program with code behind in C# to add row into table dynamically and the program worked very well in .Net Framework 1.1. When I run this program in .Net Framework 2.0 beta...
11
by: jimstruckster | last post by:
I have a table with 10 rows, I want all rows except for the first to be hidden when the page first opens up. If the user puts a value in a text box in the first row then I want the second row to...
0
by: loga123 | last post by:
Hi All, I am very new to .net and trying to develop a small application. I am using asp .net 2.0. I have a table control (server) in my code that has header row like this. <asp:Table...
7
by: leiño | last post by:
Hi, i am adding table rows dynamically with javascript. This works fine. The table is inside a div with scrolls, initially with 6 rows: ..... <div style='overflow:auto; width:100%;...
11
by: sanju | last post by:
Hi all, I am new in the world of javascript. Someone plz help me. I have two tables containing 30 rows each. In first table there is checkbox and in the second table there is radio buttons ahead...
3
by: nigelesquire | last post by:
Please help! I'm trying to clone and delete multiple rows with JavaScript. I need two delete buttons that work...! I only have one for now, but it's not working properly, the output count is...
5
by: truptivk | last post by:
Hello all, I have a question and I'd be glad if any of you could help me :) I have written code to add rows dynamically to an existing table. When the user first comes to the page, there are 2...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.