0

I am trying to list all the mysql databases and their respective tables, I am currently using this, but can anybody recommend if there is any better way.

$q = $this->db->query('SHOW DATABASES');
$databases = $q->result_array();

foreach($databases as $db) {
  $this->db->query('USE '. $db['Database']);

  $q = $this->db->query('SHOW TABLES');
  $tables = $q->result_array();             
}

1 Answer 1

2

You can use the information_schema special database, which has tables that describe all the other databases, tables, and columns.

That way you only need 1 query:

SELECT table_schema, table_name
FROM information_schema.tables
ORDER BY table_schema, table_name;
Sign up to request clarification or add additional context in comments.

2 Comments

I am a learner can you please tell me whether this information_schema table is available for all default MySQL Installations? Also is it available v4+ or v5+?
@Shishant it was only added to mysql starting in version 5.0.2: dev.mysql.com/tech-resources/articles/mysql-datadictionary.html - before that you should use SHOW as you have already.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.