Jump to content

Retrieve data from mySQL database


rob0225

Recommended Posts

I can really use some help on this.  I am a php virgin and cannot figure out this issue.

 

I need to retrieve data from a mySQL table and display it on a webpage.  I have a survey on my website with the results going to a mySQL db.  I would like to display the results of that survey.

 

My table has 7 fields and the value can be anyone of 5 different options that the user taking the survey can select.

 

I would like to display the results of each question as a percentage. I.E.

 

1. Food Quality-  Excellent    Very Good    Good      Fair      Poor

                          75%              20%        3%        2%      0%

 

 

and so on and so on for each of the questions I asked.

 

Help please! :)

 

 

Thanks,

 

Robert

Link to comment
Share on other sites

Robert, I think a bit more info about your database is needed, I have stuff flying through my brain, because I think i can see what you need, but before I give you anything, i would like to know more about the structure of your db so that i dont get you all confused

Link to comment
Share on other sites

Lodius,

 

I have 1 table with 10 fields in my database.  7 of which are answers to the questions I asked on my survey. (the other 3 are name, email and comments)

 

Of each of the 7 questions a person has the option of selecting one of 5 answers from a drop down box.

 

I only want to be able to display the combined results of the 7 questions as a percentage. I.E. for question # 1, 50% of the people answered "Excellent", 25% answered "Very Good", 15% answered "Good", 5% answered "Fair" and 5% answered "Poor".

 

The actual percentages will obviously vary based upon the actual answers but was used to give you an idea of what I am trying to accomplish.  I would need the code to calculate the percentages based upon the query.

 

Thanks,

 

Robert

Link to comment
Share on other sites

ok thats what i was envisioning

 

<?php

//to get how may people answered what
$excellent = mysql_query("SELECT question_1 FROM table WHERE question_1 = 'Excellent'");
$excelllent_count = count(mysql_fetch_assoc($excellent));

//to get total number of responses
$total = mysql_query('SELECT question_1 FROM table');
$total_count = count(mysql_fetch_assoc($count));

$result = ($excellent_count/$total_count)*100;
printf( "%2d\% of people think question 1 is excellent"); // the \% should be displayed as a % sign, but im tired, might require tweaking
?>

 

 

that is how to do 1 answer for 1 question, $total of course is reusable for all answers to the same question, there are definately better ways to do this but I think it will work, though untested, oh and it will not round up or down on the percentages, so there is the possibility that your percentages wont add up to 100

 

i use a dba so using mysql_fetch_assoc might not be necesary but i dont think it will hurt either, guess you could replace count(mysql_fetch_assoc( with mysql_numrows.....Im tired

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.