The ORDER BY clause is used to sort the result set in ascending or descending order. By default, it sorts records in ascending order. To sort in descending order, use the DESC keyword.
| SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC |
The following example retrieves the id, firstname, and lastname columns from the MyGuests table, with the records ordered by the lastname column:
| <?php $servername = “localhost”; $username = “username”; $password = “password”; $dbname = “myDB”; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die(“Connection failed: “ . $conn->connect_error); } $sql = “SELECT id, firstname, lastname FROM MyGuests ORDER BY lastname”; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo “id: “ . $row[“id”]. ” – Name: “ . $row[“firstname”]. ” “ . $row[“lastname”]. “<br>”; } } else { echo “0 results”; } $conn->close(); ?> |
Code lines explained from the example above:
First, we define an SQL query to select the id, firstname, and lastname columns from the MyGuests table, ordering the records by the lastname column. The subsequent line executes the query and stores the resulting data in a variable named $result.
Next, the num_rows() function checks if any rows were returned.
If rows are present, the fetch_assoc() function retrieves all the results into an associative array, which we then iterate through using a while() loop to display the id, firstname, and lastname columns.
The following example demonstrates the same process using the MySQLi procedural approach:
| <?php $servername = “localhost”; $username = “username”; $password = “password”; $dbname = “myDB”; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die(“Connection failed: “ . mysqli_connect_error()); } $sql = “SELECT id, firstname, lastname FROM MyGuests ORDER BY lastname”; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) { echo “id: “ . $row[“id”]. ” – Name: “ . $row[“firstname”]. ” “ . $row[“lastname”]. “<br>”; } } else { echo “0 results”; } mysqli_close($conn); ?> |
You can also display the results in an HTML table:
| <?php $servername = “localhost”; $username = “username”; $password = “password”; $dbname = “myDB”; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die(“Connection failed: “ . $conn->connect_error); } $sql = “SELECT id, firstname, lastname FROM MyGuests ORDER BY lastname”; $result = $conn->query($sql); if ($result->num_rows > 0) { echo “<table><tr><th>ID</th><th>Name</th></tr>”; // output data of each row while($row = $result->fetch_assoc()) { echo “<tr><td>”.$row[“id”].“</td><td>”.$row[“firstname”].” “.$row[“lastname”].“</td></tr>”; } echo “</table>”; } else { echo “0 results”; } $conn->close(); ?> |
The following example uses prepared statements to select the id, firstname, and lastname columns from the MyGuests table. The records are ordered by the lastname column and displayed in an HTML table:
| <?php echo “<table style=’border: solid 1px black;’>”; echo “<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>”; class TableRows extends RecursiveIteratorIterator { function __construct($it) { parent::__construct($it, self::LEAVES_ONLY); } function current() { return “<td style=’width:150px;border:1px solid black;’>” . parent::current(). “</td>”; } function beginChildren() { echo “<tr>”; } function endChildren() { echo “</tr>” . “\n”; } } $servername = “localhost”; $username = “username”; $password = “password”; $dbname = “myDBPDO”; try { $conn = new PDO(“mysql:host=$servername;dbname=$dbname”, $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $conn->prepare(“SELECT id, firstname, lastname FROM MyGuests ORDER BY lastname”); $stmt->execute(); // set the resulting array to associative $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { echo $v; } } catch(PDOException $e) { echo “Error: “ . $e->getMessage(); } $conn = null; echo “</table>”; ?> |