title: Sorting a MySQL query with ORDER BY or with PHP sort functions
body: <p>I have a query that I want to sort alphabetically, but the trick is that I want the sorting to treat two columns equally.  For instance, if the first row of <code>first_col</code> equals <code>apple</code> and the second row of <code>second_col</code> equals <code>aardvark</code> I want the value in the second row of <code>second_col</code> to be listed <strong>before</strong> the value in the first row of <code>first_col</code>.  A value (not <code>NULL</code> or <code>''</code>) will always exist in every row of <code>second_col</code>, but the value in <code>first_col</code> can be <code>''</code>.  Hopefully I have explained this good enough.  I don't care if I have to use MySQL or PHP for this, but once sorted, the array is read through and echoed into an HTML table.  Any thoughts?  </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>This is what I have for code right now.  In my MySQL query I need <code>b_name</code> and <code>l_name</code> to be equal.  The column <code>b_name</code> does not always have a value.  When I put the values into the table it is based on the existence of <code>b_name</code>.  If <code>b_name</code> does not exist the <code>f_name</code> and <code>l_name</code> are combined to replace <code>b_name</code>.</p>
  
  <pre><code>                $query = "SELECT * FROM customers ORDER BY b_name, l_name";
                  $result = mysql_query($query);
                  mysql_close($link);
  
                  $num = mysql_num_rows($result);                         
  
                  for ($i = 0; $i &lt; $num; $i++){
  
                      $row = mysql_fetch_array($result);
  
                      $class = (($i % 2) == 0) ? "table_odd_row" : "table_even_row";
  
                      if($row[b_name]!=''){
                          echo "&lt;tr class=".$class."&gt;";
  
                              echo "&lt;td&gt;&lt;a href=Edit_Customer.php?c_id=".$row[c_id]."&gt;".$row[c_id]."&lt;/a&gt;&lt;/td&gt;";
                              echo "&lt;td&gt;&lt;a href=Edit_Customer.php?c_id=".$row[c_id]."&gt;".$row[b_name]."&lt;/a&gt;&lt;/td&gt;";
                              echo "&lt;td&gt;&lt;a href=Edit_Customer.php?c_id=".$row[c_id]."&gt;".$row[phone]."&lt;/a&gt;&lt;/td&gt;";
  
                          echo "&lt;/tr&gt;";
  
                      }
  
                      else{
                          echo "&lt;tr class=".$class."&gt;";
  
                              echo "&lt;td&gt;&lt;a href=Edit_Customer.php?c_id=".$row[c_id]."&gt;".$row[c_id]."&lt;/a&gt;&lt;/td&gt;";
                              echo "&lt;td&gt;&lt;a href=Edit_Customer.php?c_id=".$row[c_id]."&gt;".$row[f_name]." ".$row[l_name]."&lt;/a&gt;&lt;/td&gt;";
                              echo "&lt;td&gt;&lt;a href=Edit_Customer.php?c_id=".$row[c_id]."&gt;".$row[phone]."&lt;/a&gt;&lt;/td&gt;";
  
                          echo "&lt;/tr&gt;";
  
                      }
  
  
                  }
  
              ?&gt;      
  
          &lt;/table&gt;
  </code></pre>