PHP MySQL Use The ORDER BY Clause
PHP MySQL Use The ORDER BY Clause
Select and Order Data
From a MySQL Database
The ORDER BY clause is used to sort the result-set
in ascending or descending order.
The ORDER BY clause sorts the records in ascending
order by default. To sort the records in descending order, use the DESC
keyword.
SELECT column_name(s) FROM table_name
ORDER BY column_name(s) ASC|DESC
To learn more about SQL, please visit our SQL
tutorial.
Select and Order Data
With MySQLi
The following example selects the id, firstname and
lastname columns from the MyGuests table. The records will be ordered by the
lastname column:
Example (MySQLi Object-oriented)
<?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 to explain from the example above:
First, we set up the SQL query that selects the id,
firstname and lastname columns from the MyGuests table. The records will be
ordered by the lastname column. The next line of code runs the query and puts
the resulting data into a variable called $result.
Then, the function num_rows() checks if there are more than zero rows returned.
If there are more than zero rows returned, the
function fetch_assoc() puts all the results into an associative
array that we can loop through. The while() loop loops through the result set and outputs
the data from the id, firstname and lastname columns.
The following example shows the same as the example
above, in the MySQLi procedural way:
Comments
Post a Comment