|
We use Order By Keyword to sort the records in a recordset. The general syntax is as under. Syntax:
SELECT columnname(s) FROM tablename ORDER BY columnname SortOrder Example: Table Name(empinfo)
FirstName
| LastName
| Email
| DOB
| Steve
| Martin
| smart.steve@studiesinn.com
| 08-04-1988
| David
| Fernandas
| davidfernandas@studiesinn.com
| 04-16-1977
| Ajay
| Rathor
| ajayrathor@studiesinn.com
| 08-24-1986
|
$con = mysql_connect("localhost:3306","adam","123456"); if (!$con) { die('Could not connect: ' . mysql_error());
} mysql_select_db("mydb", $con); $result = mysql_query("SELECT * FROM empinfo order by FirstName asc"); while($row = mysql_fetch_array($result)) { echo $row['FirstName'] . " " . $row['LastName'] . “ “ $row[‘DOB]; echo " "; } mysql_close($con); ?>
The Output will be Ajay Rathor 08-24-1986 David Fernandas 04-16-1977 Steve Martin 08-04-1988 Note:
SortOrder is optional if you does not use it the records will be sorted in ascending order (1 to 10 or a to z). Use DESC as SortOrder to sort records in descending order. RAND() is used for random sort. SQL statements are not case sensitive. You can use both capital letters and small letter. |