Php 在mysql的一行中,如何按年份降序显示前端数据

Php 在mysql的一行中,如何按年份降序显示前端数据,php,Php,我试图以降序排列年份,并将其与从php中的mysql数据库中获取的其他数据关联起来 My Table username familyperson1 yearofbirth1 cityname1 familyperson2 yearofbirth2 cityname2 familyperson3 yearofbirth3 cityname3 ... ptbarn John 1985

我试图以降序排列年份,并将其与从php中的mysql数据库中获取的其他数据关联起来

My Table
username   familyperson1     yearofbirth1     cityname1     familyperson2     yearofbirth2     cityname2      familyperson3   yearofbirth3   cityname3 ...
ptbarn     John              1985             New York      Smit              2001             San Fransisco  Kate            1966           Houston   ...
My mysql获取查询:

require("connect.php");
$srno = $_SESSION['username'];
$query = "SELECT * FROM my_table_name WHERE username='" . $srno . "'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$username = $row['username'];
$familyperson1 = $row['familyperson1'];
$yearofbirth1 = $row['yearofbirth1'];
$cityname1 = $row['cityname1'];
$familyperson2 = $row['familyperson2'];
$yearofbirth2 = $row['yearofbirth2'];
$cityname2 = $row['cityname2'];
$familyperson3 = $row['familyperson2'];
$yearofbirth3 = $row['yearofbirth2'];
$cityname3 = $row['cityname2'];
...
}
我请求你不要对我的QLI发表评论,我打算以后再修改

下面是我从mysql数据库获取的查询

echo 'Family members of' . $username . ':<br>';
echo $familyperson1 . 'is born in the year' . $yearofbirth1 . 'and stays in the city' . $cityname1 . '<br>';
echo $familyperson2 . 'is born in the year' . $yearofbirth2 . 'and stays in the city' . $cityname2 . '<br>';
echo $familyperson3 . 'is born in the year' . $yearofbirth3 . 'and stays in the city' . $cityname3 . '<br>';
echo $familyperson4 . 'is born in the year' . $yearofbirth4 . 'and stays in the city' . $cityname4 . '<br>';
echo $familyperson5 . 'is born in the year' . $yearofbirth5 . 'and stays in the city' . $cityname5 . '<br>';
我想要的结果是:

Family members of ptbarn:
Papa is born in the year 2005 and stays in city Las Vegas.
Rony is born in the year 2002 and stays in city San Diego.
Smit is born in the year 2001 and stays in city San Fransisco.
John is born in the year 1985 and stays in city New York.
Kate is born in the year 1966 and stays in city Houston.
我尝试了以下代码(来自W3S):

这是合乎逻辑的,我做错了上面,请求帮助

添加信息:在mysql行中,每个用户的数据限制仅为5。

此rsort()仅在您的变量中排序,但您的mysql结果按asc顺序排序,因此您可以使用

SELECT name, birth FROM pet ORDER BY birth DESC;

使用arsort而不是rsort,它将按相反顺序对数组排序并维护索引关联

$yearofbirth1=1985; 
$yearofbirth2=2001; 
$yearofbirth3=1966; 
$yearofbirth4=2005;
$yearofbirth5=2002;

$numbers = array($yearofbirth1, $yearofbirth2, $yearofbirth3, 
$yearofbirth4, $yearofbirth5);
arsort($numbers);

print_r($numbers);

//Array ( [3] => 2005 [4] => 2002 [1] => 2001 [0] => 1985 [2] => 1966 )

$familyperson1 = "a";
$familyperson2 = "b";
$familyperson3 = "c";
$familyperson4 = "d";
$familyperson5 = "e"; 

foreach($numbers as $k=>$v) {
    echo ${'familyperson' . ($k+1)} . ' is born in the year' . $numbers[$k] ;  
    echo "<br>";
}

这个代码有味道,我想您可以在查询中使用
orderby
,使用
LIMIT 5
@TimBiegeleisen,如果数据在列中,我想使用orderby,对吗?但是,先生,数据是在行中的。那么您应该规范化您的数据并将其放入行而不是列中。您只需复制粘贴的代码即可。。如果你看到了,我已经使用了rsort。我已经根据你的请求编辑了我的问题,并添加了mysql获取数据。请看一看。未定义的变量:familyperson5try now…我已更改了变量名称乐于帮助:)
Family members of ptbarn:
John is born in the year 2005 and stays in city New York.
John is born in the year 2002 and stays in city New York.
John is born in the year 2001 and stays in city New York.
John is born in the year 1985 and stays in city New York.
John is born in the year 1966 and stays in city New York.
SELECT name, birth FROM pet ORDER BY birth DESC;
$yearofbirth1=1985; 
$yearofbirth2=2001; 
$yearofbirth3=1966; 
$yearofbirth4=2005;
$yearofbirth5=2002;

$numbers = array($yearofbirth1, $yearofbirth2, $yearofbirth3, 
$yearofbirth4, $yearofbirth5);
arsort($numbers);

print_r($numbers);

//Array ( [3] => 2005 [4] => 2002 [1] => 2001 [0] => 1985 [2] => 1966 )

$familyperson1 = "a";
$familyperson2 = "b";
$familyperson3 = "c";
$familyperson4 = "d";
$familyperson5 = "e"; 

foreach($numbers as $k=>$v) {
    echo ${'familyperson' . ($k+1)} . ' is born in the year' . $numbers[$k] ;  
    echo "<br>";
}
d is born in the year2005
e is born in the year2002
b is born in the year2001
a is born in the year1985
c is born in the year1966