Counting the number of items in a MySQL database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jleampark
Forum Newbie
Posts: 16
Joined: Wed Jul 27, 2005 6:46 am

Counting the number of items in a MySQL database

Post 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!
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Code: Select all

SELECT COUNT(`updates`) FROM `updatesTable`
jleampark
Forum Newbie
Posts: 16
Joined: Wed Jul 27, 2005 6:46 am

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You use the MySQL count function in the query, not the PHP count() function in the code.
jleampark
Forum Newbie
Posts: 16
Joined: Wed Jul 27, 2005 6:46 am

Post by jleampark »

well, that DOES explain a lot.

Mucho thanks-o.
Post Reply