Using PHP to find database table information

Discussion in 'PHP' started by Mayhem Design, Jul 23, 2007.

  1. #1
    Hi guys,

    I'd like to know if there is an easy way to use PHP to return from the database what format each of the columns in a certain table is?

    For example, I can get it to return all the rows, that's easy ... but is there a way to find out if column 1 is a int(11) or if column 3 is varchar, and how many max characters it can hold? Or even to tell me what the primary key is and any unique columns?

    Thanks in advance for your help.
     
    Mayhem Design, Jul 23, 2007 IP
  2. sdemidko

    sdemidko Member

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #2
    mysql_fetch_field
    read the manual - this function is what you need.
     
    sdemidko, Jul 24, 2007 IP
  3. Nikolas

    Nikolas Well-Known Member

    Messages:
    1,022
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    150
    #3
    SHOW FIELDS FROM `table`;
     
    Nikolas, Jul 24, 2007 IP
  4. Wickedfingers

    Wickedfingers Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    DESCRIBE `tablename` also works. Pulled the same result for me.
     
    Wickedfingers, Jul 24, 2007 IP
  5. Mayhem Design

    Mayhem Design Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for the pointers so far.

    I tried DESCRIBE tablename and also SHOW FIELDS FROM tablename but in both these cases I only get 'Resource id #2'. Any ideas why this is?

    Thanks again.
     
    Mayhem Design, Jul 24, 2007 IP
  6. Mayhem Design

    Mayhem Design Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    OK, My bad ... I figured that bit out.

    Another question ... If a field is a binary this only shows that it is type: string. Is there any way to find out if it is binary?

    Also, the max_length that it is reporting is obviously the length of the longest object in that filed, not the max characters allowed. Is there any way to find this out?

    Thanks again.
     
    Mayhem Design, Jul 24, 2007 IP
  7. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #7
    Brewster, Jul 25, 2007 IP