Jump to content

Recommended Posts

A tables id is usually its primary key and should be unique across that entire table. Some tables may contain an id as a reference back to a "foreign" table, these are foreign keys. So in your case you would have something kinda of like this:

 

table: groups
-----------------
id | group
-----------------
0  | root
1  | wheel
2  | nobody

table: users
----------------
id | username | group_id
------------------------
0  | root     | 0
1  | admin    | 1
2  | michael  | 1
3  | gob      | 1
4  | buster   | 2

 

So as you can see in users table, group_id can be repeated (and thats fine). To query the DB and return username and groupname of the user, you would do:

SELECT group.groups, username.users FROM groups, users WHERE group_id.users = id.groups

this is helpful fo course but it's not what am trying to do. I dont want to show the relation between two tables even though they are. All i want to do if to be able to count how many records have the same content.

 

for example if you have category_id which can have more then one record with the same id in. I want to be able to say how many records in the table have the same category id. Hope this makes sence

SELECT COUNT(*) FROM yourTable WHERE category_id = 4;

Unless of course you mean something else, the sentence you said has four possible interpretations, I chose the easiest one because I'm not done with my coffee yet.  Show data, and show the kind of result you're looking for.

Okay, now we're getting somewhere.

 

You need to fetch the results after you run the query, just like normal:

 

$query = "SELECT COUNT(*) FROM table WHERE category_id = $catID";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

echo "Number of entries: " . $row[0];

Finally it works, thank you guys! hahaha sorry for the poor explanations!

Honestly, if you really were sorry, you would have fixed them.  Read how to write a smart question.  You were asked to clarify 3 times and you refused.  That's not a "haha sorry" situation.
Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.