Junius L
Junius L

Reputation: 16142

order database tables by creation time

hi i want to list all my tables from a database and order them by time, how can i do that so far i can list all my tables from the database but i can't order them by time;

$dbname = "album";

if (!mysql_connect("localhost", "root", "")) {
echo "Could not connect to mysql";
exit;
}

$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);

if (!$result) {
echo "DB Error, could not list tables\n";
echo "MySQL Error: " . mysql_error();
exit;
}
$sorts = array();
while ($row = mysql_fetch_row($result)) {
echo "Table: {$row[0]}"."<br>";
}

mysql_free_result($result);

Upvotes: 0

Views: 1092

Answers (3)

Junius L
Junius L

Reputation: 16142

this might be the solution

select * from information_schema.columns
order by create_time, column, table;

Upvotes: 0

Burak Ozdemir
Burak Ozdemir

Reputation: 5332

This may help you:

$sql = "
SELECT create_time, table_name
FROM information_schema.tables 
WHERE table_schema = '$dbname'
ORDER BY create_time
";

Also change while loop's inner part as below:

while ($row = mysql_fetch_array($result)) {
    echo "Table: {$row['table_name']} - {$row['create_time']} <br/>";
}

Upvotes: 3

J-Dizzle
J-Dizzle

Reputation: 3191

Use the information_schema tables

select * from information_schema.columns
order by create_time, column_name, table_name;

Or if you want a more narrow resultset:

select column_name, table_name, data_type, character_maximum_length,
is_nullable, column_default, create_time
from information_schema.columns
order by create_time, column_name, table_name;

Upvotes: 0

Related Questions