View Full Version : Using PHP & Mysql to count


hdshngout
07-27-2005, 04:03 AM
is this at all possible, my site, users make guilds, and when they join a guild, they get a variable added to their info in my users database. Is there a way to use php to go through the users database, and list all of the members with a certin variable, so i can put a members count on each guild. They have it on neopets, and i think it would be kinda cool to have on my site

ex

Guild Name Guild ID # of Members

Guild 1 1 5 members
Guild 4 4 8 members

for guild one, the added variable would be a 1, i need the php code to go through the database and find all of the users with a 1 in their information, and then post a number. I am learning php, and i could use all the help i could get. Thank you for your help. :)

bejayel
07-27-2005, 08:42 PM
since counting them is easy, i will show you how to do that first.

$query = "SELECT * FROM members WHERE 'Guild ID' = 1";
$result = mysql_query($query, $connection);


There are <? echo mysql_num_rows($result); ?> members in this guild.

Right now i dont have time to write out how to do the rest. When i get back from work i may be able to work on it.

tunafishy
07-27-2005, 11:39 PM
Here this will count how many users are in a guild (guild is the db, and $guild_id is the id of the guild they're viewing):

$db = mysql_connect("localhost", "usr", "pass");
mysql_select_db("guild", $db);

$guild_count=0;

$result = mysql_query("select * FROM guilds WHERE id='$guild_id'", $db);
while($row=mysql_fetch_array($result)){
$guild_count++;
}
echo "There $guild_count members for this guild";

* I forgot to say guilds is the table with the guild users

tunafishy
07-28-2005, 12:07 AM
* I forgot to say guilds is the table with the guild users

bejayel
07-28-2005, 09:11 AM
while the above will work, it can also be exponentially slower. Think if if you have 9000 users. That would take a few seconds to run.

tunafishy
07-28-2005, 08:02 PM
Ya, thats true.