Jump to content

Recommended Posts

Mysql:

 

ID | brunch | dance | game

 

Id is auto increment.

 

Brunch, dance and game have only the choose of "N" or "Y", null is not present in this case.

 

I would like to count the number of "Y" 's in column brunch.

Count the number of "y" 's in column dance, same for game.

 

How do I do this?

 

database = wwpknights

table = homecoming

 

please help. I'm a noob.

Link to comment
https://forums.phpfreaks.com/topic/66969-mysql/
Share on other sites

<?php

// Connects the script to MYSQL
require_once("connect.php");

// Select database
mysql_select_db(wwpknights);

// Select whole table
$result_set = mysql_query ( "SELECT * FROM homecoming" );

// Count how many people submitted info
$number_of_fields = mysql_num_fields ( $result_set );

// Echo the number of people...

echo "$number_of_fields is the number of people who submitted info";



// Counting Separate Elements


// Branch Yes
$brunch_sel = mysql_query("SELECT * FROM homecoming WHERE brunch='Y'");
$brunch = mysql_num_fields ( $brunch_sel );

// Dance Yes
$dance_sel = mysql_query("SELECT * FROM homecoming WHERE dance='Y'");
$dance = mysql_num_fields ( $dance_sel );

// Game Yes
$game_sel = mysql_query("SELECT * FROM homecoming WHERE game='Y'");
$game = mysql_num_fields ( $game_sel );



// Echo number of yes in each category activity

echo "$brunch are attending the branch";
echo "$dance are attending the dance";
echo "$game are attending the game";

?>

 

No error, it just gives me all 5 for the echo statements. While there is a lot more than 5 total fields and def not equal y in all categories! What did I do wrong?

Link to comment
https://forums.phpfreaks.com/topic/66969-mysql/#findComment-335861
Share on other sites

To elaborate on pocobueno1388's post...

 

<?php

$query="SELECT count(*) as brunch_count FROM homecoming WHERE brunch='Y'";
$result=mysql_query($query);
$row=mysql_fetch_rows($result);

$brunch_total=$row->brunch_count;

echo 'There are '. $brunch_total .' y\'s in the brunch column';
?>

 

 

Make sure you use the as NAME in queries your using count otherwise you will not have a "handle" to grab the results by.

 

Nate

Repeat for each column

Link to comment
https://forums.phpfreaks.com/topic/66969-mysql/#findComment-335940
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.