Home > Article > Backend Development > How to sort using MySQL's ORDER BY clause in PHP
In MySQL, the ORDER BY clause can be used with the SELECT statement to sort data for a specific field in order; it can sort the result set in ascending or descending order. Let's take you through a brief introduction to the basic method of sorting using MySQL's ORDER BY clause in PHP. I hope it will be helpful to you.
Basic syntax
Basic syntax for the ORDER BY clause:
SELECT 字段名 FROM 表名 ORDER BY 字段名 ASC/DESC(升序或降序)
Note: In the ORDER BY clause, ASC is the default and can be omitted, indicating ascending order. [Recommended related video tutorials: MySQL Video Tutorial]
##Usage Example
1. Simply sort by the age field in ascending order
<?php header("content-type:text/html;charset=utf-8"); $link = mysqli_connect("localhost", "root", "", "mydb"); //连接数据库 mysqli_set_charset($link,"utf8"); if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); } $sql = "SELECT * FROM demo ORDER BY age"; if($res = mysqli_query($link, $sql)){ if(mysqli_num_rows($res) > 0){ echo "<table>"; echo "<tr>"; echo "<th>name</th>"; echo "<th>age</th>"; echo "<th>sex</th>"; echo "</tr>"; while($row = mysqli_fetch_array($res)){ echo "<tr>"; echo "<td>" . $row['name'] . "</td>"; echo "<td>" . $row['age'] . "</td>"; echo "<td>" . $row['sex'] . "</td>"; echo "</tr>"; } echo "</table>"; mysqli_free_result($res); } else{ echo "找不到匹配的记录。"; } } else{ echo "错误:无法执行 $sql. " . mysqli_error($link); } mysqli_close($link); ?>Output:
Code description:
The "res" variable stores the data returned by the function mysql_query(). Every time mysqli_fetch_array() is called, it returns the next row from the res() set. The while loop is used to traverse all rows of the table "demo".2. Use the object-oriented method to sort in descending order through the ORDER BY clause
<?php header("content-type:text/html;charset=utf-8"); $link = new mysqli("localhost", "root", "", "mydb"); mysqli_set_charset($link,"utf8"); if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); } $sql = "SELECT * FROM demo ORDER BY age DESC"; if($res = mysqli_query($link, $sql)){ if(mysqli_num_rows($res) > 0){ echo "<table>"; echo "<tr>"; echo "<th>name</th>"; echo "<th>age</th>"; echo "<th>sex</th>"; echo "</tr>"; while($row = mysqli_fetch_array($res)){ echo "<tr>"; echo "<td>" . $row['name'] . "</td>"; echo "<td>" . $row['age'] . "</td>"; echo "<td>" . $row['sex'] . "</td>"; echo "</tr>"; } echo "</table>"; mysqli_free_result($res); } else{ echo "找不到匹配的记录。"; } } else{ echo "错误:无法执行 $sql. " . mysqli_error($link); } mysqli_close($link); ?>Output: and above That’s the entire content of this article, I hope it will be helpful to everyone’s study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !
The above is the detailed content of How to sort using MySQL's ORDER BY clause in PHP. For more information, please follow other related articles on the PHP Chinese website!