Results 1 to 3 of 3

Thread: select the column names from a table.

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default select the column names from a table.

    how do you select the columns from a table and display the names of the columns in a given table?

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    here's the answer I came up with:
    Code:
    $result = mysql_query("show columns from tablename");
    $count = 0;
    while ($row=mysql_fetch_row($result)){
    $name.="$row[0] ";
    }
    $name=explode(" ",$name);
    $count_n=count($name);
    $a=0;
    while ($a<$count_n-1){
    echo "$name[$a]<br>";$a++;
    }

  3. #3
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    I have a solution pre-built, hooray for you.
    You should be able to get the gist of what to do with this. If you don't, you can just use my database class (link in signature).
    PHP Code:
    function getColumns($table) {
            
    $rr = array();
            
    $r $this->query("SELECT * FROM information_schema.columns".
                
    " WHERE `TABLE_NAME`='$table'",1);
            foreach(
    $r as $key => $info) {
                
    $len $info["COLUMN_TYPE"]; // int(5) <-- 5 is our length
                
    $len explode("(",$len); // The easiest way I know to get something
                
    $len $len[1];              // between two strings is to explode twice and get either side.
                
    $len explode(")",$len);
                
    $len $len[0];
                
    $rr[$info["COLUMN_NAME"]] = array(
                    
    "Column Name" => $info["COLUMN_NAME"],
                    
    "Data Type" => $info["DATA_TYPE"],
                    
    "Length" => $len,
                    
    "Null" => ($info["IS_NULLABLE"] == "YES")
                );
            }
            return 
    $rr;
        } 

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
  •