PHP如何对数据进行排序,这里需要用到PHP默认的ORDER BY方法。
ORDER BY使用方法
ORDER BY SELECT column_name(s) FROM table_name
ORDER BY默认对记录进行升序排序,如果你想降序排序,请使用 DESC 关键字。
ORDER BY SELECT column_name(s) FROM table_name DESC
实例代码
下面的实例选取 "Persons" 表中存储的所有数据,并根据 "Age" 列对结果进行排序:
<?php $con=mysqli_connect("localhost","username","password","database"); // 检测连接 if (mysqli_connect_errno()) { echo "连接失败: " . mysqli_connect_error(); } $result = mysqli_query($con,"SELECT * FROM Persons ORDER BY age"); while($row = mysqli_fetch_array($result)) { echo $row['FirstName']; echo " " . $row['LastName']; echo " " . $row['Age']; echo "<br>"; } mysqli_close($con); ?>
以上结果将输出:
Glenn Quagmire 33Peter Griffin 35
本文转载自:精准像素:11px.cn ,感谢作者分享实用知识