PHP mysql ORDER by keyword has an important role in PHP, this article will explain in detail the PHP mysql order by keyword related knowledge.
ORDER by keyword
The ORDER BY keyword is used to sort the data in the recordset.
The ORDER BY keyword defaults to sorting the records in ascending order.
If you want to sort in descending order, use the DESC keyword.
Grammar
SELECT column_name (s) from table_name
ORDER by column_name (s) asc| DESC
To learn more about SQL, please visit our SQL Tutorial.
Instance
The following instance selects all the data stored in the "Persons" table and sorts the results according to the "Age" column:
<?php$con=mysqli_connect ("localhost", "username", "password", "database");//Detect connection if (Mysqli_connect_errno ()) { echo "Connection failed:". Mysqli_connect_error ();} $result = Mysqli_query ($con, "select * from Persons"), and while ($row = Mysqli_fetch_array ($result)) { echo $ro w[' FirstName ']; echo "". $row [' LastName ']; echo "". $row [' age ']; echo "<br>";} Mysqli_close ($con);? >
The above results will be output:
Glenn Quagmire 33Peter Griffin 35
Sort by two columns
You can sort by more than one column. When sorting by more than one column, the second column is used only when the first column has the same value:
SELECT column_name (s) from table_name
ORDER by Column1, Column2
This article explains in detail the PHP MySQL Order by keyword related content, more study materials clear concern PHP Chinese network can watch.