How to use Order By keyword in MySQL through PHP

In this article I am going to explain about order by keyword in MySQL.
  • 1991

Order By keyword in MySQL PHP

PHP MySQL order by keyword specifies how to sort the rows in the result set. If you include order by clause, the rows in the result set are sorted in the specified sequence. Otherwise, the row are returned in same sequence as they appear in the base table. In most of cases, that means they are returned in primary key sequence. By default order by keyword represent records in ascending order.

Syntax of order by keyword

SELECT columnName from table name ORDER BY columnName asc or desc

Example of order by keyword

<html>

<body>

<?php

$con = mysql_connect("localhost","gupta","sharad");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("Mcn_Solution", $con);

 

$result = mysql_query("SELECT * FROM Emp order by Id");

 

while($row = mysql_fetch_array($result))

  {

  echo $row['Fname'] . " " . $row['LastName'] . " ". $row['Id'];

  echo "<br />";

  }

 

mysql_close($con);

?>

</body>

</html>


Output:

orderby.jpg

You may also want to read these related articles :
here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.