MySQLi 환경에서 특정 테이블 모든 필드명 구하기

<?php
function get_field_names($table, $link)
{
    $columns = array();

    $sql = " select * from `$table` limit 1 ";
    $result = mysqli_query($link, $sql);

    while($field = mysqli_fetch_field($result)) {
        $columns[] = $field->name;
    }

    return $columns;
}

$link = mysqli_connect($host, $user, $pass, $db);

if(!$link) {
    die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}

$row = get_field_names('member', $link);

print_r($row);
?>

MySQLi 환경에서 특정 테이블의 모든 필드명을 mysqli_fetch_field 함수를 이용해 구하는 방법이다.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.