title: php code order mysql query table by column name
body: <p>I want, using php code, to order a mysql query table by column names (witch are taken "dynamically" - column names are not fixed).
  The script only gets the correct column name of each column from the query.</p>
  
  <p>...etc...connect to db..</p>
  
  <pre><code>    $query = "SELECT a.name,a.surname,b.email,c.phone 
        FROM users as a 
        inner join users_email as b 
        inner join users_phone as c
        WHERE a.id=b.id AND a.id=c.id ";
  
  
  if (isset($_GET['orderby'])&amp;&amp;
      (($_GET['orderby']=='name') || ($_GET['orderby']=='surname')||($_GET['orderby']=='email')))
  {
      $query .= "ORDER BY" . $_GET['orderby'] . "ASC;";
  }
  
  $result = mysql_query($query,$con);
  
  echo "&lt;table&gt;&lt;table border=1&gt;&lt;tr&gt;";
  
  for($i=0; $i &lt; mysql_num_fields($result); $i++){
      $field_info = mysql_fetch_field($result, $i);
  
      echo '&lt;th&gt;&lt;a href="login3.php?orderby='. $field_info -&gt; name .'" &gt;'. $field_info -&gt; name .'&lt;/a&gt;&lt;/th&gt;';
  }
  
  while ($row=mysql_fetch_assoc($result)){
      echo "&lt;tr&gt;";
      foreach($row as $_column){
      echo "&lt;td&gt;{$_column}&lt;/td&gt;";
      }
      echo "&lt;/tr&gt;";
  }
  
  echo "&lt;/table&gt;";
  
  mysql_close($con);
  
  ?&gt;
  </code></pre>