Home Php php MySQL order by clause

 
 

Share on Google+Share on Google+

php MySQL order by clause

Advertisement
In SQL generally we use three clauses, select ,from, and where.

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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 17, 2009

Related Tutorials

Discuss: php MySQL order by clause  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com