Code2Design.com

User login

Programming

The Layout

Navigation

Popular content

Resources

Who's online

There are currently 1 user and 7 guests online.

Online users

  • Plompatotmeta

SELECT COUNT from database

I thought I would just take a minute to share this little code snippet with you as I had some trouble finding posts online that would tell me if I was going about this the right way! The problem is that I needed a fast way to count how many rows were in a database without using a CPU demanding function like mysql_num_rows().

<?php
$query 
'SELECT COUNT(*) FROM `users`';
$result mysql_query($query) or die('Sorry, we could not count the number of results: ' mysql_error());
$numberofresults mysql_result($result0);
?>

You can also limit the count to only rows that meet a certain value:

<?php
//Where the user id is greater than 149.
$query 'SELECT COUNT(*) FROM `your_table` WHERE `id` > 149';
$result mysql_query($query) or die('Sorry, we could not count the number of results: ' mysql_error());
$numberofresults mysql_result($result0);
?>

Keep in mind that this is for counting all the rows in the table. For example, if you had a database like this with 40 of your "users" in it:

CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(20) NOT NULL default '',
`password` varchar(20) NOT NULL default '',
`email` varchar(70) NOT NULL default '',
)

Even though there are 4 fields in each members row - the result of the above script will only output "40" not "160". Because while there are a total of 160 columns in all - there are only 40 users (each with the above four columns in their row).

Remember, it is slower to select a single field (SELECT COUNT(id)FROM `users`) than it is to just count the whole thing (SELECT COUNT(*)FROM `users`). Because MySQL already knows how many rows are in each database - so it can just tell you. But if you specify a certain field - then MySQL has to open each row and look around for that field.


Submitted by David on May 14, 2007 - 12:16am.
printer friendly version

GREAT!

Thx! This was just what I were looking for!! thx again!


SELECT JOIN with COUNT of details for each header

I have a select of header and detail records, which i would like to return the count of details for each header. Using the below i get an error

SELECT TOP 1000 ab.*, abd.Key, COUNT(abd.UPC) as cnt FROM AuditBatches as ab INNER JOIN AuditBatchesDetail as abd ON ab.Key = abd.Key WHERE (ab.Dept = '3')

Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC Microsoft Access Driver] You tried to execute a query that does not include the specified expression 'Key' as part of an aggregate function., SQL state 37000 in SQLExecDirect

Any help would be appreciated,
Steve


Bad Key

Like the error states - it doesn't like the "abd.Key" and "ab.Key = abd.Key" parts. Is "Key" the name of one of your table columns? Or are you using it when you should be using "ab.id = abd.id"?


sect * from

How to extract just a Xls sheet from a database table.?


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati