Use SHOW DATABASES to list databases visible to the current MySQL account, and SHOW TABLES to list tables and views visible in the selected database.
SHOW DATABASES;
SHOW TABLES;
List databases visible to the current MySQL account:
SHOW DATABASES;
The result contains one database name per row.
SHOW DATABASES lists every database on the server. Visibility depends on the privileges assigned to the current MySQL account.To see which database is currently selected:
SELECT DATABASE() AS current_database;
If no database is selected, DATABASE() returns NULL.
You can select a database in SQL with:
USE sql_tutorial;
USE statement is often unnecessary in PHP code.List tables and views in the currently selected database:
SHOW TABLES;
If no database has been selected, MySQL cannot know which database's tables you want to list.
You can name the database explicitly:
SHOW TABLES FROM sql_tutorial;
This is useful when the connection can access several databases and you do not want to change the current database.
Use LIKE to filter table names.
For example, list names beginning with analytics:
SHOW TABLES LIKE 'analytics%';
The percent sign matches any sequence of characters.
SHOW TABLES FROM sql_tutorial
LIKE 'analytics%';
_ is a one-character wildcard. If you specifically need a literal underscore in a more complex pattern, escape it according to the SQL mode and pattern syntax being used.Plain SHOW TABLES lists object names but does not tell you whether each object is a base table or a view. Use:
SHOW FULL TABLES;
The result includes a second column describing the object type, such as BASE TABLE or VIEW.
For precise filtering and portable column names, INFORMATION_SCHEMA.TABLES is usually clearer:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_TYPE = 'BASE TABLE'
ORDER BY TABLE_NAME;
To inspect the SQL definition MySQL stores for a table:
SHOW CREATE TABLE student;
This is useful for reviewing:
See Copy Table and SHOW CREATE TABLE.
INFORMATION_SCHEMA.TABLES provides structured metadata and is useful when you need filtering, sorting, or additional table properties.
SELECT TABLE_NAME,
TABLE_TYPE,
ENGINE
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'sql_tutorial'
ORDER BY TABLE_NAME;
SELECT TABLE_NAME,
TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = DATABASE()
ORDER BY TABLE_NAME;
Using DATABASE() avoids hard-coding the database name when the current connection already selected it.
The original tutorial used INFORMATION_SCHEMA.TABLES to order names. That remains a good approach:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'sql_tutorial'
ORDER BY TABLE_NAME ASC;
You can combine sorting with a name filter:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'sql_tutorial'
AND TABLE_NAME LIKE 'analytics%'
ORDER BY TABLE_NAME ASC;
See ORDER BY.
The original page demonstrated a dropdown containing database names. Here is the same idea with safe HTML output:
<?php
require "config.php";
$stmt=$dbo->query("SHOW DATABASES");
echo '<select name="database">';
while($row=$stmt->fetch(PDO::FETCH_NUM)){
$db=htmlspecialchars(
(string)$row[0],
ENT_QUOTES,
'Windows-1252'
);
echo '<option value="'
.$db
.'">'
.$db
.'</option>';
}
echo '</select>';
See PHP PDO database connection.
For the database already selected by the PDO connection:
<?php
require "config.php";
$stmt=$dbo->query("SHOW TABLES");
while($row=$stmt->fetch(PDO::FETCH_NUM)){
echo htmlspecialchars(
(string)$row[0],
ENT_QUOTES,
'Windows-1252'
)
.'<br>';
}
The query contains no external data, so query() is appropriate.
Database and table names are SQL identifiers, not normal data values. PDO placeholders cannot be used like this:
-- This does not parameterize an SQL identifier
SHOW TABLES FROM :database
If an application lets a user choose from databases returned by SHOW DATABASES, validate the submitted name against a server-side allowlist before using it as an identifier.
The output of metadata commands depends on MySQL privileges.
SHOW DATABASES may show only databases for which the account has privileges.SHOW TABLES shows objects visible to the current account in the selected database.SHOW CREATE TABLE also requires sufficient access to inspect the table definition.If an expected table is absent from the result, first confirm the selected database and the account's privileges before assuming the table does not exist.
Select a database in the connection or use SHOW TABLES FROM database_name.
Results depend on the current account's privileges.
Views can also appear. Use SHOW FULL TABLES or INFORMATION_SCHEMA.TABLES when object type matters.
The result column is named according to the database, for example Tables_in_sql_tutorial. When you need portable filtering and sorting, INFORMATION_SCHEMA provides stable column names such as TABLE_NAME.
_ means one character in a LIKE pattern. Use a simpler prefix such as 'analytics%' when that is the real requirement, or escape a literal underscore deliberately.
Identifiers cannot be safely bound with normal PDO value placeholders. Use fixed names or a strict allowlist.
Use SHOW DATABASES. The databases shown depend on the privileges of the current MySQL account.
Use SHOW TABLES after selecting a database through the connection or USE statement.
Use SHOW TABLES FROM database_name if the current account has permission to see that database.
Use SHOW TABLES LIKE 'prefix%' for a simple name filter, or query INFORMATION_SCHEMA.TABLES for more flexible filtering and sorting.
Use SHOW FULL TABLES or inspect TABLE_TYPE in INFORMATION_SCHEMA.TABLES.
Use SHOW CREATE TABLE table_name.
No. Placeholders bind data values, not SQL identifiers such as database or table names. Use fixed or strictly allowlisted identifiers.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.