ravi
ravi

Reputation: 109

PHP MYSQL : way to list table in ascending order

we use " SHOW TABLES FROM " 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

Upvotes: 2

Views: 4580

Answers (2)

supernickman
supernickman

Reputation: 24

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>";
}

Upvotes: 0

George Cummins
George Cummins

Reputation: 28926

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

Upvotes: 5

Related Questions