Kodr.F
Kodr.F

Reputation: 14390

PHP MSSQL PDO get table names?

i want to get array of tables from remote database using PDO ( MSSQL ) i notice i cannot use mysql query commands , any advice how to get all tables information from mssql database using pdo or mssql ?

i am able to connect :

try {
    $hostname = $myServer;
    $port = 10060;
    $dbname = $myDB ;
    $username = $myUser ;
    $pw = $myPass;
    $dbh = new PDO ("mssql:host=$hostname;dbname=$dbname","$username","$pw");
  } catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
  }

but not able to get data :

$q = $dbh->prepare("DESCRIBE");
$q->execute();
$table_fields = $q->fetchAll(PDO::FETCH_COLUMN);
print_r($table_fields);

  } catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
  }

user permissions i am using has only read permission

Upvotes: 1

Views: 2629

Answers (1)

Neil P
Neil P

Reputation: 3190

query against the information_Schema.

select Table_Name, table_type from information_schema.tables

You can use the columns table to get the name + data types for columns etc.

select TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, data_type, CHARACTER_MAXIMUM_LENGTH 
from INFORMATION_SCHEMA.COLUMNS

Upvotes: 2

Related Questions