PHP Bab 28 PHP Mysql Order
PHP Bab 28 PHP Mysql Order
Syntax
SELECT column_name(s)
FROM table_name
ORDER BY column_name
Note: SQL statements are not case sensitive. ORDER BY is the same as order by.
Example
The following example selects all the data stored in the "Person"
table, and sorts the result by the "Age" column:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
while($row = mysql_fetch_array($result))
{
echo $row['FirstName']
echo " " . $row['LastName'];
echo " " . $row['Age'];
echo "<br />";
}
mysql_close($con);
?>
Glenn Quagmire 33
Peter Griffin 35
28-1
If you use the ORDER BY keyword, the sort-order of the recordset is ascending by default (1
before 9 and "a" before "p").
SELECT column_name(s)
FROM table_name
ORDER BY column_name DESC
SELECT column_name(s)
FROM table_name
ORDER BY column_name1, column_name2
28-2