SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Zealot imagize's Avatar
    Join Date
    Oct 2004
    Location
    Australia
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Show all tables in a field (PostgreSQL)

    PHP offers a function to show all tables in a specific database for a mysql database only.

    I am currently working with PostgreSQL and I was wondering is there a similar function that will display every table in a database. (im looking for a php function, i dont want to use shell etc)

  2. #2
    Woof! Woof! elel1138's Avatar
    Join Date
    Apr 2004
    Location
    Warsaw, Poland
    Posts
    202
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unfortunately postgres doesn't have an easy way of doing it. In psql it's easy, just use \dt command. To do it programatically, you need a very complicated query (you really don't wanna know ).

    Fortunately, the DB library from PEAR does have a solution for this (example from documentation).

    PHP Code:
    <?php
    // Once you have a valid DB object named $db...
    $data $db->getListOf('tables');

    if (
    DB::isError($data)) {
        die(
    $data->getMessage());
    }

    echo 
    implode("\n"$data);
    ?>
    Leszek Leszczyński
    www.hazelsoft.com
    --- Real programmers don't document. If it was hard to write,
    it should be hard to understand. ---

  3. #3
    SitePoint Guru
    Join Date
    Jan 2004
    Location
    Uppsala, sverige
    Posts
    698
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Code:
    select * from information_schema.tables
    does not seem so complicated

    http://www.postgresql.org/docs/8.0/i...ma-tables.html

  4. #4
    Woof! Woof! elel1138's Avatar
    Join Date
    Apr 2004
    Location
    Warsaw, Poland
    Posts
    202
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [QUOTE=swampBoogie]
    Code:
    select * from information_schema.tables
    Hmm, actually you're right and I'm not. Let me add that this returns views also, which of course can be filtered by "where table_type='BASE TABLE'". However, if I understand correctly, this is valid for version 8?

    When I needed to access the table information I found a solution with "pg_class" table, which was really complicated. This must have been a long time ago.
    Leszek Leszczyński
    www.hazelsoft.com
    --- Real programmers don't document. If it was hard to write,
    it should be hard to understand. ---


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •