php MySQL order by clause:
In SQL generally we use three clauses, select ,from, and where. There are many more clauses are present in SQL which are used for different purpose. Order by is one of the important clause of SQL. Order by clause is used to sort a particular field in either ascending order or descending order.
General Format of SQL is as follows:
select <list of fields to be displayed or * for displaying all fields>
from <table name>
where <condition if any>
order by <field name> <asc-for ascending order, it is default order or desc-for descending order>
Example:
<?php
$db=@mysql_connect("localhost","root","");
if(!$db)
{
die("Can not establish connection".mysql_error());
}
mysql_select_db("roseindia",$db);
$result=mysql_query("select * from student order by name asc");
while($row=mysql_fetch_array($result))
{
echo $row['age'];
echo" ".$row['name'];
echo"<br/>";
}
?>
Output:
28 rahul
28 shantanu
27 vikash
26 vinay