Jump to content

mysqli count


cartaysm

Recommended Posts

I want to count column values in a few different tables with different filters...

 

I can do the long way which would be like this;

 

$result = $mysqli->query("SELECT COUNT(Gender) FROM table where Gender = 'F'");

$Female = $result->fetch_row();

 

$result = $mysqli->query("SELECT COUNT(Class) FROM table2 where Class = 'SprintT'");

$ST = $result->fetch_row();

 

$result = $mysqli->query("SELECT COUNT(Class) FROM table2 where Class = 'SprintD'");

$SD = $result->fetch_row();

 

Which allows to me edit the page anyway I want, but seems like a waste to connect to the database 100 times just for the data when I could do something like below.

 

 

Or I can search the follow way;

$builder = "SELECT ";

$builder .= "b.Gender, b.State, b.Country, ";

$builder .= "d.Class, d.Category, d.ShirtSize, d.Hear, d.AmountPaid, d.Discounts, d.Status ";

$builder .= "FROM table = b ";

$builder .= "LEFT OUTER JOIN table2 = d ";

$builder .= "on b.id = d.ID";

$stmt = $mysqli->query($builder);

 

$registeredinfo = array_count_values($stmt->fetch_assoc());

foreach ($registeredinfo as $key => $val) {

print "$key = $val <br />";

}

Produces;

M = 1

Ohio = 1

USA = 1

SprintT= 1

AgeGroup = 1

PreviouslyParticipated = 1

 

But this doesn’t seem to go through all lines I pull, example if I had 2 lines instead of one I would expect the numbers to change to 2 but they remain at 1.

 

Anyone have any ideas how I pull a lot of categories and then count them efficiently?

Link to comment
Share on other sites

Thank you for your response. That isnt exactly what I was looking for, the place you added a counter is working fine, that pulls a single column and counts it. The problem I am having is that if I use that select statement i will have to use about 100 different statements which seems like a waste.

 

The other option is below where I iterate through an array that was built with joins, the problem is that it only pulls 1 record. I could try and add a counter to the iteration to see if produces more values. Is that what you meant?

 

$registeredinfo = array_count_values($stmt->fetch_assoc());

foreach ($registeredinfo as $key => $val) {

print "$key = $val <br />";

}

Link to comment
Share on other sites

  • 2 weeks later...

As it turns out I finally found the answer just posting it for anyone else that stumble accross this

 

$result = $mysqli->query("SELECT Category from Table");
$arr = array();
while ($states = $result->fetch_row()){
foreach ($states as $state => $value) {
$arr[] = $value;
}
}
$arr = array_count_values($arr);
foreach ($arr as $key => $value) {
print("".$key.": ".$value."");
}

 

This code will pull multiple rows and then split and count each row...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.