So I know with a standard mysql call we can do mysql_list_tables
, however is there an equivalent while using PDO? If so, does this return an array? Thanks!
Using the PHP fetchAll() method with the query() method If a query doesn't accept a parameter, you can fetch all rows from the result set as follows: First, execute the query by calling the query() method of the PDO object. Then, fetch all rows from the result set into an array using the fetchAll() method.
PDOStatement::fetchAll() returns an array containing all of the remaining rows in the result set. The array represents each row as either an array of column values or an object with properties corresponding to each column name. An empty array is returned if there are zero results to fetch.
Execute the query with PDO::query()
:
SHOW TABLES;
If you fetch an associative array, the name of the column will be:
Tables_in_databasename
Note: this will list both tables and views. If you must get only tables, use this instead:
SELECT
TABLE_NAME
FROM information_schema.TABLES
WHERE
TABLE_TYPE='BASE TABLE'
AND TABLE_SCHEMA='yourdatabasename';
Do $pdo->query("show tables");
to obtain a result set of tables contained in the current database.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With