title: How can i display Mysql entries in a specific order
body: <p>In my android app i am trying to view a list of friends in the order of who is online and who is not 
  The code i am using to do it is extracting the request flag (1) from friends table and then use the friend_id to get data from users table and use json_encode to send data back to app in order to display it , the order of friends is displayed in the same order they are in users table :
  Here is the code :</p>
  
  <pre><code>if(isset($_POST['list_friends'])) {
      $sql = "SELECT friend_id FROM friends WHERE user_id='$user_id' &amp;&amp; request='1'";
      $res = mysqli_query($connection,$sql) or die(mysqli_error());
      $row_to_json = array();
      while($rows = mysqli_fetch_assoc($res)) {
          foreach($rows AS $row) {
              $sql2 = "SELECT * FROM users WHERE id='$row'
                      ORDER BY FIELD(is_online,'1') DESC";
              $res2 = mysqli_query($connection,$sql2) or die(mysqli_error());
              while ($rows2 = mysqli_fetch_assoc($res2)) {
              //echo $rows_to_json = json_encode((object)$rows2);
                  $row_to_json[] = (($rows2));
              }
          }
      }
      echo json_encode(($row_to_json));
  }
  </code></pre>
  
  <p>is_online field is either (0 if offline and 1 if online)
  Any idea of what is wrong in this code ?</p>
  
  <p><strong>EDIT 1 :</strong></p>
  
  <p>I tried this code : </p>
  
  <pre><code>    SELECT
    friends.friend_id,users.is_online
  FROM friends INNER JOIN users ON friends.user_id = users.id
  WHERE
    (friends.user_id = 21 and request = 1)
  ORDER BY
   (users.is_online) DESC
  </code></pre>
  
  <p>I added <code>users.is_online</code> to check for the actual output of this field and this is what is got :
  <a href="https://i.stack.imgur.com/d48Yc.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/d48Yc.jpg" alt="Actual mysql result from query"></a></p>
  
  <blockquote>
    <p>Note that the actual is_online value exists in users table for <code>**22**</code> and
    <code>**27**</code> is <code>**0**</code> not <code>**1**</code> !</p>
  </blockquote>
  
  <p><strong>EDIT 2 :</strong></p>
  
  <p>**</p>
  
  <blockquote>
    <p>Perfect :)</p>
  </blockquote>
  
  <p>**</p>
  
  <p><a href="https://i.stack.imgur.com/uXCwF.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uXCwF.jpg" alt="Exactly what i need"></a></p>