Automatically detecting tables in a db

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bissatch@yahoo.co.uk

    Automatically detecting tables in a db

    Hi,

    Is it possible to write a script that, when provided with a database,
    it will return all the tables names?

    I will go onto try and put together a script that will not only output
    the table names but also output the sql code required to CREATE the
    table (i.e. CREATE TABLE example (exampleid auto_increment not null
    primary key integer, name text, email text);) - Is this pretty simple
    to do? Are there any free scripts online that would do this? Cheers

    Burnsy

  • jerry gitomer

    #2
    Re: Automatically detecting tables in a db

    bissatch@yahoo. co.uk wrote:[color=blue]
    > Hi,
    >
    > Is it possible to write a script that, when provided with a database,
    > it will return all the tables names?
    >
    > I will go onto try and put together a script that will not only output
    > the table names but also output the sql code required to CREATE the
    > table (i.e. CREATE TABLE example (exampleid auto_increment not null
    > primary key integer, name text, email text);) - Is this pretty simple
    > to do? Are there any free scripts online that would do this? Cheers
    >
    > Burnsy
    >[/color]

    Burnsy

    Yes! The exact script to list the tables in a database depends
    on the database you are using. For example in MySQL the script
    would include:

    $sql1 = "SHOW TABLES";
    $res1 - mysql_query($sq l1, $db) or die(ms_err_msg( db,
    $mysql_error));



    Following is an example of a create table script. Edit it to
    use your table column names and characteristics .


    $sql2 = "DROP TABLE IF EXISTS mbr";
    $res2 = mysql_query($sq l2, $db) or die(ms_err_msg( $db,
    $mysql_error));


    $sql3 = "CREATE TABLE mbr (
    email varchar(80) NOT NULL,
    uname varchar(32) NOT NULL,
    password varchar(32) NOT NULL,
    refemail varchar(80),
    balance int(11) NOT NULL,
    mtype char(1) NOT NULL,
    zip varchar(11) NOT NULL,
    lastvisit varchar(10),
    PRIMARY KEY (email))";
    $res3 = mysql_query($sq l3, $db) or die(ms_err_msg( $db,
    $mysql_error));


    ms_err_msg is a function that displays a caption, the mysql
    error, and closes the database.

    HTH
    Jerry



    Comment

    • BKDotCom

      #3
      Re: Automatically detecting tables in a db

      Look at
      SHOW CREATE TABLE
      and SHOW TABLES

      Comment

      • ZeldorBlat

        #4
        Re: Automatically detecting tables in a db

        If you're using MySQL check out phpMyAdmin, as it already does what you
        want to do:



        Comment

        • JDS

          #5
          Re: Automatically detecting tables in a db

          On Fri, 01 Jul 2005 06:56:35 -0700, bissatch wrote:
          [color=blue]
          > Is it possible to write a script that, when provided with a database,
          > it will return all the tables names?[/color]

          Yes

          --
          JDS | jeffrey@example .invalid
          | http://www.newtnotes.com
          DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

          Comment

          Working...