我想使用 php 代码按列名对 mysql 查询表进行排序(女巫被“动态”采用 - 列名不固定)。该脚本仅从查询中获取每列的正确列名。
...等...连接到数据库..
$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'])&&
(($_GET['orderby']=='name') || ($_GET['orderby']=='surname')||($_GET['orderby']=='email')))
{
$query .= "ORDER BY" . $_GET['orderby'] . "ASC;";
}
$result = mysql_query($query,$con);
echo "<table><table border=1><tr>";
for($i=0; $i < mysql_num_fields($result); $i++){
$field_info = mysql_fetch_field($result, $i);
echo '<th><a href="login3.php?orderby='. $field_info -> name .'" >'. $field_info -> name .'</a></th>';
}
while ($row=mysql_fetch_assoc($result)){
echo "<tr>";
foreach($row as $_column){
echo "<td>{$_column}</td>";
}
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>