How do I get column names from a given MySQL table?

前端 未结 2 932
走了就别回头了
走了就别回头了 2020-12-10 06:44

How do I get the column names of a table into a PHP array using the mysqli extension? I need to fetch the column names of any given table without fetching any data from the

2条回答
  •  没有蜡笔的小新
    2020-12-10 07:15

    You can use array_keys() to get all the keys of an array,

    $myarray = array('key1' => 'a', 'key2' => 'b')
    $x = array_keys($myarray);
    

    The result you want can be obtained from $x

    $x = array(0 => 'key1', 1 => 'key2');
    

    Inorder to get the column names of a table,

    $sql = "SELECT * FROM table_name LIMIT 1";
    $ref = $result->query($sql);
    $row = mysqli_fetch_assoc($ref);
    $x = array_keys($row);
    

    now $x array contains the column names of the table

提交回复
热议问题