728x90

PHP 를 이용하여 MySQL 칼럼명을 가져오는 코드를 필요했다.

테이블을 INNER JOIN 하면서 select * from tableA a, tableB b where a.uid=b.uid 로 하면 tableA 와 tableB의 모든 칼럼을 가져오게 된다.


가져오고 싶은 것은 tableA의 칼럼만 가져오고 싶어서 함수로 구현했다.


<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/dbconnect.php'; //db접속

echo getColumns('member_table'); // table name

function getColumns($table){

    global $DB_CONNECT;
    $fields = array();
    $res=mysql_query("SHOW COLUMNS FROM ".$table."");
    while ($x = mysql_fetch_assoc($res)){
      array_push($fields,$x['Field']);
    }

    for($i = 0; $i < count($fields); $i++){
        $columns = join($fields,",");
    }
    return $columns;
}

?>


블로그 이미지

Link2Me

,