PHP MySQL Order By

Google Advertisements

Prev Tutorial Next Tutorial

Display Record from Database in Decending Order

The order by clause is used to fetch data in ascending order or descending order on the basis of column.

mysql_query() is used to excecte selected query. In below example variable sql is passed inside mysql_query(), it means excecute selected query when this function called.

Below query used to select data from user table in ascending order on the basis of user_id column.

Syntax

$sql = "SELECT * FROM  letter_dispatch ORDER BY  user_id"; 

Below query used to select data from user table in descending order on the basis of user_id column.

Syntax

$sql = "SELECT * FROM  letter_dispatch ORDER BY  user_id DESC"; 

connect-db.php

Connect to database

<?php

$server = 'localhost';
$user = 'user_name';
$pass = 'password';
$db = 'database_name';

// Connect to Database

$connection = mysql_connect($server, $user, $pass)
or die("Could not connect to server ... \n" . mysql_error ());

// select database
mysql_select_db($db)
or die("Could not connect to database ... \n" . mysql_error ());

?>

create-table.php

Create Table in database

<?php

include 'connect-db.php';

$sql = "SELECT * FROM  letter_dispatch ORDER BY  user_id DESC";  
$result = mysql_query($sql, $connection);

?>

<table>
<tr><th>user_id</th><th>First Name</th><th>Last Name</th></tr>

<?php

if ($result)
 {
  while($row = mysql_fetch_array($result))
 {
?>

<tr>
<td><?php echo $row["user_id"]; ?></td>
<td><?php echo $row["f_name"]; ?></td>
<td><?php echo $row["l_name"]; ?></td>
</tr>

<?php
 }
} 
?>
</table>

mysql_close($conn);

?>

Output


Prev Tutorial Next Tutorial

Google Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. If you want to post your article on our website/FB Group/FB Page please contact on hitesh.xc@gmail.com or 8076671483

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 8076671483

Path variable is set for providing path for all Java tools like java, javac, javap, javah, jar, appletviewer which are used in java programming. All these tools are available in bin folders so we set path upto bin folders. Difference between path and classpath in Java

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar. Difference between path and classpath in Java

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user. Cloud Computing Tutorial

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects. Download Java C C++ Projects

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: hitesh.xc@gmail.com or 8076671483 Try this Keyword C++ Programs