<html>
<head></head>
<body>
<?

echo "The last modification date of this file is ",
        date('j-n-Y H:i:s',filemtime(__FILE__)), " ","<br>\n";

$date = getdate(filemtime(__FILE__));
echo "This file was last modified on the following day of the year: $date[yday]<br>\n";
echo "<hr \>\n";

@$d = mysql_connect("localhost","your db_username","your_db_password") or die("Can't connect to the database.");
@mysql_select_db("your_database") or die("Can't select database.");

// Retrieve records sorted in descending order and display them:
$q = "select * from class order by grade DESC";
@$r = mysql_query($q) or die("Can't perform query.");
echo "Here are the records in the table: <br>\n";
echo "Name,	Grade<br>\n";
while ($row = mysql_fetch_array($r))
{
	echo ucwords(strtolower($row[name])), ", $row[grade]<br>\n";
}
echo "<hr \>";

// Display the number of records:
echo "There are ", mysql_num_rows($r), " records in the table.<br>\n";
echo "<hr \>";

// Now find the average and display it:
$q = "select avg(grade) from class";
@$r = mysql_query($q) or die("Can't perform query.");
$row = mysql_fetch_array($r);
$average_grade = $row[0];
// Or you can write: 
$average_grade = $row['avg(grade)'];
echo "The average grade is: ", "$average_grade.<br>\n";

// Another way to do the above:
$q = "select avg(grade) as average from class";
/* OR */ // $q = "select avg(grade) from class as average";
@$r = mysql_query($q) or die("Can't perform query.");
$row = mysql_fetch_array($r);
$average_grade = $row['average'];
echo "The average grade is: ", "$average_grade.<br>\n";
echo "<hr \>";

// Now find the highest grade and display it:
$q = "select max(grade) from class";
@$r = mysql_query($q) or die("Can't perform query.");
$row = mysql_fetch_array($r);
$highest_grade = $row[0];
// Or you can write:
$highest_grade = $row['max(grade)'];

echo "The highest grade is: ", "$highest_grade.<br>\n";

// Another way to do the above:
$q = "select max(grade) as highest from class";
/* OR */ // $q = "select max(grade) from class as heighest";
@$r = mysql_query($q) or die("Can't perform query.");
$row = mysql_fetch_array($r);
$highest_grade = $row['highest'];
echo "The highest grade is: ", "$highest_grade.<br>\n";
echo "<hr \>";

// Retrieve all students who got the highest grade and display them:
$q = "select name from class where grade = $highest_grade";
@$r = mysql_query($q) or die("Can't perform query.");
echo "The following students got the highest grade:<br>\n";
while ($row = mysql_fetch_array($r))
{
	echo ucwords(strtolower($row[name])), "<br>\n";
}

@mysql_close($d) or die("Can't close the database");

?>
</body>
</html>
