Share on Google+Share on Google+

Php Sql ORDER BY

Advertisement
 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:

 

Advertisements

Posted on: December 10, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

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.