We have discussed some of the mathematical functions in previous tutorials but above functions are very common in MySQL.Here also I have taken progress table.Codes for above functions are in left column and results are in right column.
COUNT
<?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_database", $con); $result = mysql_query("SELECT COUNT(*) AS total FROM progress "); echo "<table border='1'> <tr> <th>Month</th> <th>Nuber of Products</th> </tr>"; while($row = mysql_fetch_array($result)) { $num_rows = $row['total']; echo "<tr>"; echo "<td>" . $row['Month'] . "</td>"; echo "<td>" . $row['Production'] . "</td>"; echo "</tr>"; } echo "</table>"; echo $num_rows."rows found"; mysql_close($con); ?> |
|
<?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_database", $con); $result = mysql_query("SELECT MAX(Production) FROM progress "); while($row = mysql_fetch_array($result)) { echo "maximum production is:-".$row['MAX(Production)']; } echo "</table>"; mysql_close($con); ?> | maximum production is:-32 |
<?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_database", $con); $result = mysql_query("SELECT MIN(Production) FROM progress "); while($row = mysql_fetch_array($result)) { echo "minimum production is:-".$row['MIN(Production)']; } echo "</table>"; mysql_close($con); ?> | minimum production is:-10 |
<?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_database", $con); $result = mysql_query("SELECT AVG(Production) FROM progress "); while($row = mysql_fetch_array($result)) { echo "Average production is:-".$row['AVG(Production)']; } echo "</table>"; mysql_close($con); ?> | Average production is:-21 |
<?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_database", $con); $result = mysql_query("SELECT SUM(Production) FROM progress "); while($row = mysql_fetch_array($result)) { echo "Sum of production is:-".$row['SUM(Production)']; } echo "</table>"; mysql_close($con); ?> | Sum of production is:-252 |
No comments:
Post a Comment