如何在PHP中按升序排列下表

时间:2011-05-19 15:37:54

标签: php mysql

我无法按升序排列输出表ORDER BY ASC 在显示表中工作但与表元素一起工作
我的代码如下:

<?php  
 $result = mysql_query("SHOW TABLES FROM st_db_1");
 while($row = mysql_fetch_row($result) ) {
     $result_tb = mysql_query("SELECT id FROM $row[0] LIMIT 1");
     $row_tb=mysql_fetch_array($result_tb);

     //checking for availbility of result_tb
     if (!$result_tb) {
              echo "DB Error, could not list tables\n";
              echo 'MySQL Error: ' . mysql_error();
              exit;
     }
     echo "<a href=exa.php?id=$row_tb[id]>{$row[0]}<br/><a/>" ;
 }
 mysql_free_result($result);
 mysql_free_result($result_tb);
?>

3 个答案:

答案 0 :(得分:1)

select  TABLE_NAME
from    information_schema.TABLE_CONSTRAINTS
order by TABLE_NAME ASC;

订购table_names。

请参阅(位于页面底部)http://bugs.mysql.com/bug.php?id=14473

答案 1 :(得分:0)

$result_tb = mysql_query("SELECT id FROM $row[0] LIMIT 1 s ORDER BY id ASC");

你必须使用ORDER BY

答案 2 :(得分:0)

如果你的意思是那个

SHOW TABLES FROM st_db_1 ORDER BY x ASC

不起作用,因为没有这样的语法(http://dev.mysql.com/doc/refman/5.0/en/show-tables.html)。但是,您可以通过PHP对结果进行排序,然后选择每个表:

<?php  
$result = mysql_query("SHOW TABLES FROM st_db_1");
$tableArr = array();

while($row = mysql_fetch_row($result)) {
    $tableArr[] = $row;
}

usort($tableArr, function cmp($a, $b) {
    if ($a[0] == $b[0])
        return 0;
    return ($a[0] < $b[0]) ? -1 : 1;
}

foreach($tableArr as $row) {
    $result_tb = mysql_query("SELECT id FROM $row[0] LIMIT 1");
    $row_tb = mysql_fetch_array($result_tb);

    //checking for availbility of result_tb
    if (!$result_tb) {
        echo "DB Error, could not list tables\n";
        echo 'MySQL Error: ' . mysql_error();
        exit;
    }
    echo "<a href=exa.php?id=$row_tb[id]>{$row[0]}<br/><a/>";
}

mysql_free_result($result);
mysql_free_result($result_tb);