这是我的表结构,
我想显示为
我的意思是,第二个coloumn(名字)应该是升序,第三个coloumn(地方)应该是降序。
我想要的是
名称应该在Asc Order中,然后在右边,Place应该是Desc Desc
我到目前为止所尝试的是
我该怎么做?
<?php
include ('conn.php');
$sql="SELECT * FROM test";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
while($rows=mysql_fetch_array($result))
{
foreach($result as $k=>$v)
{
echo $k;
}
}
?>
它将结果显示为为每个参数提供的无效参数。我正在做的错误是什么以及如何实现我的输出
答案 0 :(得分:1)
要修复排序,只需添加ORDER BY名称,放置
即可然后还有几个问题导致无法正常工作:
$rows
,而不是$result
(这是“无效参数”错误的来源)。你可能想做这样的事情来解决这些问题:
<?php
include('conn.php');
$sql = "SELECT * FROM test ORDER BY name, place";
$result = mysql_query($sql);
while ($rows = mysql_fetch_array($result)) {
foreach ($rows as $k => $v) {
echo "$k is $v. ";
}
echo "<br/>";
}
?>
答案 1 :(得分:0)
那你为什么不在你的查询中做一个订单,比如
SELECT * FROM test order by name;
答案 2 :(得分:0)
您只能修改查询。
使用此查询:
SELECT * FROM test ORDER BY name ASC, place DESC
答案 3 :(得分:0)
对于您的错误,您使用错误的变量作为foreach。 替换这个 -
foreach($result as $k=>$v)
{
echo $k;
}
用这个 -
foreach($rows as $k=>$v)
{
echo $k;
}
购买为什么在循环中使用foreach。你可以得到像 -
这样的价值观while($rows = mysql_fetch_array($result)) {
echo $rows['name'].' '.$rows['place'].'<br/>';
}
并且不使用mysql_*
功能。请改用mysqli_*
对于您的预期输出,请尝试使用以下查询 -
SELECT * FROM `test` order by name asc, place desc
答案 4 :(得分:0)
这是你可以使用array_multisort修复的东西。 试试下面的例子(我还没有测试过,但应该工作)
<?php
include ('conn.php');
$sql="SELECT * FROM test";
$result=mysql_query($sql);
$totals = array();
$row = array();
$places = array();
while($row=mysql_fetch_array($result)){
$totals[] = $row
$names[] = $row['name'];
$places[] = $row['place'];
}
array_multisort( $names, SORT_ASC, $places, SORT_DESC, $totals );
// now you can use $totals, which is sorted as you want
print_r( $totals );