www.webdeveloper.com
Results 1 to 5 of 5

Thread: Which php functions can I use to get the mysql table structures ?

  1. #1
    Join Date
    May 2006
    Posts
    245

    Which php functions can I use to get the mysql table structures ?

    Hi,

    I would like to know if there are some PHP functions that can
    spit out the table structure for me.


    I wrote a script to build my database like this:

    PHP Code:
    /*
    **********  CLIENTS TABLE  *********************
    */
    $sql "DROP TABLE IF EXISTS `clients`";
    $result mysql_query($sql)
        or die(
    "could not DROP clients."mysql_error());  

    echo 
    "DROPPED clients Table <br>";

    $sql "CREATE TABLE `clients` (
        `client_id` smallint(6) unsigned NOT NULL auto_increment,
        `agent_no` smallint(6) NOT NULL default 0,
        `live` char(1)  NOT NULL default 'n',

        ... and lots more

         KEY `email` (`email`),
         PRIMARY KEY (client_id)

    ) ENGINE=MyISAM DEFAULT CHARSET=latin1"
    ;


    $result mysql_query($sql)
        or die(
    "could not CREATE clients."mysql_error());  

    echo 
    "Created clients Table <br> date:$today_time<br>";

    // ADD DATA using:

    $sql "INSERT INTO `clients` (confirm,type,access,profile,lig_pos,contact,client_pict,email,create_date,last_date)
            VALUES 
            ('y','P','zxc345A','This is the Visitor profile','900','Visitor','dave.jpg ','Dave@test.com','
    $today_time','$today_time'),
            ('y','P','pass003','This is my Expert profile ','894','John Simmons','john.jpg','John@test.com','
    $today_time','$today_time'),
            ('y','m','rg007','This is a member profile','101','Rosalind Gardner','rosalind.jpg','rosalind@test.com','
    $today_time','$today_time')";

    $result mysql_query($sql)
             or die(
    "could not execute INSERT set up clients."mysql_error());  

    echo 
    "FILLED clients Table <br>"
    And then I do the same for other tables.

    Now since I initially created the tables,
    I have added new columns and changed some of the existing ones.

    Is there an easy way to output current table structures
    so that I can easily insert it in to my build_tables.php script ?

    Also how can I dump the current rows of data so that I can
    add them to the build_tables.php script and rebuild my table if I want/need it ?

    PS - I don't want to load a php class or package of any kind
    I would just like to use PHP commands / functions and my own scripts.

    Thanks.


    .
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    509
    But of course there is. But you have to do it the same way you built your tables - use sql, not php. Try searching the MySQL documentation and you will see how it's done.

    Of course someone else will probably tell you exactly how, thus saving you the work of doing a little research on your own. Tsk, tsk.

  3. #3
    Join Date
    May 2006
    Posts
    245
    Of course someone else will probably tell you exactly how, thus saving you the work of doing a little research on your own. Tsk, tsk.
    I hope so

    After all the point of the forum is to share and help each other.
    We could all spend all day looking at manuals and google'ing stuff
    but then this forum would be DEAD !!!

    Its more fun to ask and share and get advice from those that
    have done it before and can just type out a quick answer.

    I hope my question and any answers that come is helpful not just to me but
    to other forum users and to the forum owners ...
    ( more pages for their adverts )


    Thanks.


    .
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    509
    I did help the OP - I corrected his misconception that he needed to use PHP to interrogate MySQL. I then steered him in the direction of the way it is done.

    Re: your point about the purpose of the forum. Sure - we can all sit here and wait for every little question time and time again, or we can teach.

    You know the old adage about "give a man a fish....; teach a man to fish..."?

  5. #5
    Join Date
    Sep 2013
    Posts
    221
    To get a list of columns for a table, use the DESCRIBE SQL statement. The syntax is as follows:

    DESCRIBE TableName

    To get a list of tables on the database, use this SQL statement:

    SHOW TABLES

    or you can try out with the other approach:
    Not necessarily a single function, but you can create yourself one using:

    mysql_fetchassoc() to get the resultset as key(col name)/value(col value) pairs
    array_keys() to get all the column names from resultset and create your table header

    The below link may also help you to come out of your problem:
    http://abidibo.blogspot.in/2010/07/g...-with-php.html

    Hope this helps.
    strad solutionswww.stradsolutions.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles