Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6593787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:43:35+00:00 2026-05-25T17:43:35+00:00

I have a MySQL database table which holds 6 types of records I need

  • 0

I have a MySQL database table which holds 6 types of records I need to get the total number of records for each of the types and then create a variable for each one of the results so for example

wt = 3
spv = 33
shw = 78

and so on..

I have written this code

$tquery = "SELECT equipment, COUNT(id) AS num FROM contract GROUP BY equipment"; 
$tresult = mysql_query($tquery) or die(mysql_error());
while($trow = mysql_fetch_array($tresult)){
echo "There are ". $trow['COUNT(id)'] ." ". $trow['equipment'] ." items.";
echo "<br />";
}

The reason for needing a variable for each type is that I need to work out a percentage value to show this data in a chart, I have written this code and it works fine as long as I have the correct data from the table.

Any help would be awesome thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:43:36+00:00Added an answer on May 25, 2026 at 5:43 pm

    Firstly, where you’re referencing $trow['COUNT(id)'], this is incorrect; it should be coming back to you with the name num (as defined in your SQL query), so you should actually be using $trow['num'].

    Now to store your totals:

    Create an array to contain your totals. Lets call it $total. Define it as an empty array outside the while loop:

    $total = array();
    

    Then inside the loop, assign a new element for the $total array containing the equipment type as the key, and the count as the value:

    $total[$trow['equipment']] = $trow['num'];
    

    When the loop is finished, you should then have an array containing all the totals, which you can perform calculations on. PHP provides you with the array_sum() and count() functions (among plenty of other handy functions) which should be enough to get you started with working out averages and percentages.

    hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table which contains double values stored in mysql database ...I need to
I have a table inside of my mysql database which I constantly need to
i have a large mysql database table in which one column contains values ranging
So I have a mySQL database with a 'users' table which includes a username,
Greetings friends, In my MySQL database, I have 'MAB' table which contains information about
I have a MySQL database with multiple tables, each of which have the same
I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
I have this MySQl database that has a table which contains a column that
I have a table which show data from mysql database in HTML table like
I have a MySQL database and a table which has a names column as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.