更改mysqli_result中的行顺序

For example I have the code

$query="Select * from table order by id";
$res1=mysqli_query($link,$query);

while($row=mysqli_fetch_array($res1)) {
echo $row['Id'];
}

echo '<br>';

$query="Select * from table order by user";
$res1=mysqli_query($link,$query);

while($row=mysqli_fetch_array($res1)) {
echo $row['Id'];
}

It is possible to do this without second query and without use of array and foreach, to change orders of row in resul1 from first query to get order like in second query.

You can easily order by several fields at one time:

$query = "SELECT * FROM table ORDER BY id, user ";
$res1 = mysqli_query($link,$query);
....

you can achieve this by passing a php variable in the mysql query.

 $query="Select * from table order by $order";

and set $order as you want.

$query = "SELECT * FROM table ORDER BY id, user ";

This will order your data first by id and then user.

You can change order of results in PHP.

  1. Get all results into a PHP array.
  2. Write a comparison function.
  3. Use usort http://php.net/manual/en/function.usort.php