Php Sql ORDER BY


 

Php Sql ORDER BY

 This example illustrates how to execute query with ORDER BY clause in the php application.

 This example illustrates how to execute query with ORDER BY clause in the php application.

Php Sql ORDER BY

 This example illustrates how to execute query with ORDER BY clause in the php application.

In this example we create a php page to execute a query for select data of the table as order by name in decreasing order. SQL allows for sorting of retrieved data with the ORDER BY clause. This clause requires the column name based on which the data will be sorted. The ORDER BY clause can sort in an ASCENDING (ASC) or DESCENDING (DESC) order depending upon the argument supplied. The Complete source code and output screen shots as follows:

Table: emp

 

Source Code of sqlorderby.php 

<?php
  $con = mysql_connect("localhost","root","root");
  if (!$con) {
    die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("test", $con);
  $result = mysql_query("SELECT * FROM emp ORDER BY name desc");
  echo "<table border='1'>
    <tr>
      <th>Emp_Id</th>
      <th>Name</th>
    </tr>";
    while ($row = mysql_fetch_array($result)) {
      echo "<tr>";
      echo "<td>" . $row['emp_id'] "</td>";
      echo "<td>" . $row['name'] "</td>";
      echo "</tr>";
    }
  echo "</table>";
  
  mysql_close($con);
?>

Download Source Code

 

Output:

 

Ads