Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PDO fetch all tables

Tags:

php

mysql

pdo

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!

like image 746
grep Avatar asked Jun 22 '11 17:06

grep


People also ask

How to use fetch all in PHP?

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.

What does fetch all return PHP?

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.


2 Answers

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';
like image 63
Michael Berkowski Avatar answered Sep 29 '22 12:09

Michael Berkowski


Do $pdo->query("show tables"); to obtain a result set of tables contained in the current database.

like image 36
GordyD Avatar answered Sep 29 '22 13:09

GordyD