Home > Backend Development > PHP Tutorial > How to sort using MySQL's ORDER BY clause in PHP

How to sort using MySQL's ORDER BY clause in PHP

青灯夜游
Release: 2023-04-04 18:44:02
Original
3804 people have browsed it

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.

How to sort using MySQL's ORDER BY clause in PHP

Basic syntax

Basic syntax for the ORDER BY clause:

SELECT  字段名 FROM 表名 ORDER BY 字段名 ASC/DESC(升序或降序)
Copy after login

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

The following is a data table "demo", which contains three fields: name, age, sex. We introduce the use of ORDER BY clause through a simple example.

How to sort using MySQLs ORDER BY clause in PHP

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[&#39;name&#39;] . "</td>"; 
                echo "<td>" . $row[&#39;age&#39;] . "</td>"; 
                echo "<td>" . $row[&#39;sex&#39;] . "</td>"; 
            echo "</tr>"; 
        } 
        echo "</table>"; 
        mysqli_free_result($res); 
    } else{ 
        echo "找不到匹配的记录。"; 
    } 
} else{ 
    echo "错误:无法执行 $sql. " . mysqli_error($link); 
} 
mysqli_close($link); 
?>
Copy after login

Output:

How to sort using MySQL's ORDER BY clause in PHP

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[&#39;name&#39;] . "</td>"; 
                echo "<td>" . $row[&#39;age&#39;] . "</td>"; 
                echo "<td>" . $row[&#39;sex&#39;] . "</td>"; 
            echo "</tr>"; 
        } 
        echo "</table>"; 
        mysqli_free_result($res); 
    } else{ 
        echo "找不到匹配的记录。"; 
    } 
} else{ 
    echo "错误:无法执行 $sql. "  . mysqli_error($link); 
} 
mysqli_close($link); 
?>
Copy after login
Output:

How to sort using MySQL's ORDER BY clause in PHP

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template