Jump to content

PHP -> MySQL Simple while loop issue


mtucker6784

Recommended Posts

Hi eveyrone,

 

First off, I promise this is not any type of homework assignment, even though this probably looks really simple. We are running PHP 5.1 and MySQL 4.1.

 

I am attempting to create a site survey for one of our student development programs. I'm trying to make a result.php file which will take the SUM of the kids who took the survey (they are asked to fill in what grade they are in.)

 

I'm trying to create a while loop which will go through each grade, rather than creating a new query command per grade. What is happening is that when I execute the php file, I don't receive any error, even from mysql_error().

 

<?php

$dbhost = 'myserver';

$dbuser = 'myusername';

$dbpass = 'usernamepw';

 

$conn = mysql_connect($dbhost,$dbuser,$dbpass) or die ('Error connecting to mysql');

$dbname = 'surveys';

$i = 9;

// Declare variable results...

$query9 = "SELECT SUM(grade9) FROM surveys.cte_survey";

$queryg10 = "SELECT SUM(grade10) FROM surveys.cte_survey";

$queryg11 = "SELECT SUM(grade11) FROM surveys.cte_survey";

$queryg12 = "SELECT SUM(grade12) FROM surveys.cte_survey";

while ($i<=12)

{

$grade$i = mysql_query($query$i) or die (mysql_error());

printf("%u ",$grade$i);

$i++;

}

mysql_close($conn);

?>

 

Now if I change my while loop to use "$grade9" in to replace my "$grade$i" statement, then I receive the results "2 3 4 5". It's like it just counts how many times the $i loop executed, but not the amount of students who have taken the survey.

 

I've tried changing things in the while loop to "$grade[$i]", "$grade['$i']" "$grade$i" all without success. I have no other idea how I can approach this and I'm hoping others may have some insight.

 

Here is the data in my "grade9,grade10,grade11,grade12" columns located in my surveys.cte_survey.

mysql> SELECT SUM(grade9),SUM(grade10),SUM(grade11),SUM(grade12) from surveys.cte_survey;

+-------------+--------------+--------------+--------------+

| SUM(grade9) | SUM(grade10) | SUM(grade11) | SUM(grade12) |

+-------------+--------------+--------------+--------------+

|          3 |            3 |            2 |            1 |

+-------------+--------------+--------------+--------------+

1 row in set (0.00 sec)

 

If anyone can point me in the right direction, I would really appreciate it. Thank you for your time,

 

-MT

 

Link to comment
https://forums.phpfreaks.com/topic/135232-php-mysql-simple-while-loop-issue/
Share on other sites

It would help greatly if you posted your table structure in a mysql dump type thing..  (If you are using PHP myadmin, it's the export tab, usually default settings, then copy and paste)

 

I have a guess, but I'd like to see your structure first..

Archived

This topic is now archived and is closed to further replies.

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