MySQL query to get column names?

前端 未结 21 2055
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 01:56

I\'d like to get all of a mysql table\'s col names into an array in php?

Is there a query for this?

相关标签:
21条回答
  • 2020-11-22 02:46

    The call of DESCRIBE is working fine to get all columns of a table but if you need to filter on it, you need to use the SHOW COLUMNS FROM instead.

    Example of PHP function to get all info of a table :

    // get table columns (or return false if table not found)
    function get_table_columns($db, $table) {
        
        global $pdo;
    
        if($cols = $pdo->query("DESCRIBE `$db`.`$table`")) {
            if($cols = $cols->fetchAll(PDO::FETCH_ASSOC)) {
                return $cols;
            }
        }
        
        return false;
    }
    

    In my case, I had to find the primary key of a table. So, I used :

    SHOW COLUMNS FROM `table` WHERE `Key`='PRI';
    

    Here is my PHP function :

    // get table Primary Key
    function get_table_pk($db, $table) {
    
        global $pdo;
                
        $q = "SHOW COLUMNS FROM `$db`.`$table` WHERE `Key` = 'PRI'";
        if($cols = $pdo->query($q)) {
            if($cols = $cols->fetchAll(PDO::FETCH_ASSOC)) {
                return $cols[0];
            }
        }
        
        return false;
    }
    
    0 讨论(0)
  • 2020-11-22 02:49

    Use mysql_fetch_field() to view all column data. See manual.

    $query = 'select * from myfield';
    $result = mysql_query($query);
    $i = 0;
    while ($i < mysql_num_fields($result))
    {
       $fld = mysql_fetch_field($result, $i);
       $myarray[]=$fld->name;
       $i = $i + 1;
    }
    

    "Warning This extension is deprecated as of PHP 5.5.0, and will be removed in the future."

    0 讨论(0)
  • 2020-11-22 02:49

    i no expert, but this works for me..

    $sql = "desc MyTable";
    $result = @mysql_query($sql);
    while($row = @mysql_fetch_array($result)){
        echo $row[0]."<br>"; // returns the first column of array. in this case Field
    
          // the below code will return a full array-> Field,Type,Null,Key,Default,Extra    
          // for ($c=0;$c<sizeof($row);$c++){echo @$row[$c]."<br>";}    
    
    }
    
    0 讨论(0)
  • 2020-11-22 02:50

    The simplest solution out of all Answers:

    DESC `table name`
    

    or

    DESCRIBE `table name`
    

    or

    SHOW COLUMNS FROM `table name`
    
    0 讨论(0)
  • 2020-11-22 02:50

    if you use php, use this gist.

    it can get select fields full info with no result,and all custom fields such as:

    SELECT a.name aname, b.name bname, b.* 
    FROM table1 a LEFT JOIN table2 b
    ON a.id = b.pid;
    

    if above sql return no data,will also get the field names aname, bname, b's other field name

    just two line:

    $query_info = mysqli_query($link, $data_source);
    $fetch_fields_result = $query_info->fetch_fields();
    
    0 讨论(0)
  • 2020-11-22 02:52

    Not sure if this is what you were looking for, but this worked for me:

    $query = query("DESC YourTable");  
    $col_names = array_column($query, 'Field');
    

    That returns a simple array of the column names / variable names in your table or array as strings, which is what I needed to dynamically build MySQL queries. My frustration was that I simply don't know how to index arrays in PHP very well, so I wasn't sure what to do with the results from DESC or SHOW. Hope my answer is helpful to beginners like myself!

    To check result: print_r($col_names);

    0 讨论(0)
提交回复
热议问题