开发者

PHP MYSQL : way to list table in ascending order

we use " SHOW TABLES F开发者_Go百科ROM " to list tables from database but there is no way to list table in order (ORDER BY dont work with tables) is ther any other way to arrange it ?? can we do this with php :if yes please give me hint to do it thank you


You can use INFORMATION_SCHEMA:

SELECT table_name FROM INFORMATION_SCHEMA.TABLES
  WHERE table_schema = 'db_name'
  ORDER BY table_name ASC

Reference: http://dev.mysql.com/doc/refman/5.0/en/tables-table.html


Another option that doesn't rely on INFORMATION_SCHEMA:

Set up an array to store table options:

$tableArr = array();

Get the tables from the desired database:

$tableQuery = mysql_query("SHOW TABLES FROM databasse");

Add each table to your array:

while($row = mysql_fetch_array($tableQuery)) {
  $tableArr[] = $row[0];
}

Now sort the array using asort (alphabetical) or arsort (reverse alphabetical):

asort($tableArr);

Now you can list the tables in alphabetical order by simply looping through the array:

foreach($tableArr as $table) {
  echo "<li>$table</li>";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜