Home » Labs » PHP MySQL Order By – An Example  

PHP MySQL Order By – An Example


Filed under Labs

In PHP ORDER BY keyword is used for sort data in different methods. For example we can sort the recordset in ascending order, descending order etc. By default ORDER BY keyword sorts the records in ascending order. If you want to order the data then you can use the keyword DESC with query.

Syntax

<?php
SELECT * FROM table_name ORDER BY column_name(s) DESC
?>

PHP MySQL Order By Example

<?php
$result = mysql_query("SELECT * FROM Students ORDER BY Age");
 
 while($row = mysql_fetch_array($result)){
	   echo $row['Name'];
	   echo " " . $row['Class'];
	   echo " " . $row['Age'];
	   echo "<br />";
   }
 
?>

The output of the above code will be:

John 8th.std 12
Roy 10th.std 15

Posts you may like: