Mysql在数组中存储数据然后排序desc

时间:2015-03-06 13:36:04

标签: php mysql arrays

我有以下数组,我想用mysql的2列填充它。我试过了,但我似乎无法解决这个问题。任何帮助将不胜感激

mysql表如下:

IDNO | NAME

 <?php
$age = array(

$result2 = mysqli_query($con,"SELECT * FROM tbl_suburbs LIMIT 10");

while($row2 = mysqli_fetch_array($result2))
{       
$row2['name']=>$row2['id']
}

 "Ben"=>"37",
 "Joe"=>"43",
 );
arsort($age);

foreach($age as $x => $x_value)
 {
 echo "Key=" . $x . ", Value=" . $x_value;
 echo "<br>";
}
?>

1 个答案:

答案 0 :(得分:1)

您的目标不是100%明确。代码错了。

让我试着猜猜你到底在寻找什么:

$result2 = mysqli_query($con,"SELECT * FROM tbl_suburbs ORDER BY age DESC LIMIT 10");

$age = array();

while($row2 = mysqli_fetch_array($result2))
{       
  $age[$row2['name']]=$row2['age']; 
}

foreach($age as $x => $x_value)
 {
 echo "Key=" . $x . ", Value=" . $x_value;
 echo "<br>";
}

请知道您是否需要一些澄清和/或改进。