Page 1 of 1

Counting the number of items in a MySQL database

Posted: Thu May 31, 2007 11:31 am
by jleampark
I have a database that tracks projects, a link to the document, if there are updates and (if there are) links to the updates. The information is actually in two tables: the first contains the information on the projects with a flag to say whether or not there are updates. The update table has all the information about the updates and a column to JOIN the two databases.

How do I count (to set up a loop) the number of updates for each project?

Thanks!

Posted: Thu May 31, 2007 11:39 am
by superdezign

Code: Select all

SELECT COUNT(`updates`) FROM `updatesTable`

Posted: Thu May 31, 2007 1:00 pm
by jleampark
Oh, if it were only that easy... perhaps it is and I am just a dope. :oops:

When I try to use COUNT ($update), all I get is 1.

Let me be more specific and see if that helps...

I have the "projreg" table with:

id | proj | url | up (a binary flag)
---+-------+------+-----
1 | ProjA | urlA | 0 (no)
2 | ProjB | urlB | 1 (yes)
3 | ProjC | urlC | 1 (yes)

and I have the "updates" table with:

id | id_proj | update | u_url , where "id_proj" is the link to the "projreg" table:
---+---------+-----------+---------------
1 | 2 | UpdB1 | UpdateURL1
2 | 3 | UpdC1 | UpdateURL2
3 | 3 | UpdC2 | UpdateURL3
4 | 3 | UpdC3 | UpdateURL4


So, Project B has one update and Project C has three updates.

If I put

Code: Select all

$countup = count($id_proj);
in a loop, I just get "1" returned.

What am I doing wrong?

Posted: Thu May 31, 2007 1:11 pm
by RobertGonzalez
You use the MySQL count function in the query, not the PHP count() function in the code.

Posted: Thu May 31, 2007 1:13 pm
by jleampark
well, that DOES explain a lot.

Mucho thanks-o.