是否有一种方法可以在MySQL中使用PHP获取表的列名?


当前回答

我做了一个PDO函数,它返回一个简单数组中的所有列名。

public function getColumnNames($table){
    $sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = :table";
    try {
        $core = Core::getInstance();
        $stmt = $core->dbh->prepare($sql);
        $stmt->bindValue(':table', $table, PDO::PARAM_STR);
        $stmt->execute();
        $output = array();
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            $output[] = $row['COLUMN_NAME'];                
        }
        return $output; 
    }

    catch(PDOException $pe) {
        trigger_error('Could not connect to MySQL database. ' . $pe->getMessage() , E_USER_ERROR);
    }
}

输出将是一个数组:

Array (
[0] => id
[1] => name
[2] => email
[3] => shoe_size
[4] => likes
... )

对不起死灵,但我喜欢我的功能;)

附注:我没有包括类核心,但你可以使用自己的类。科学博士

其他回答

这对我很有效。

$sql = "desc MyTableName";
$result = @mysql_query($sql);
while($row = @mysql_fetch_array($result)){
    echo $row[0]."<br>";
}

调查:

mysql_query('DESCRIBE '.$table);

Mysqli fetch_field()为我工作:

if ($result = $mysqli -> query($sql)) {
  // Get field information for all fields
  while ($fieldinfo = $result -> fetch_field()) {
    printf("Name: %s\n", $fieldinfo -> name);
    printf("Table: %s\n", $fieldinfo -> table);
    printf("Max. Len: %d\n", $fieldinfo -> max_length);
  }
  $result -> free_result();
}

来源:https://www.w3schools.com/pHP/func_mysqli_fetch_field.asp

我做了一个PDO函数,它返回一个简单数组中的所有列名。

public function getColumnNames($table){
    $sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = :table";
    try {
        $core = Core::getInstance();
        $stmt = $core->dbh->prepare($sql);
        $stmt->bindValue(':table', $table, PDO::PARAM_STR);
        $stmt->execute();
        $output = array();
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            $output[] = $row['COLUMN_NAME'];                
        }
        return $output; 
    }

    catch(PDOException $pe) {
        trigger_error('Could not connect to MySQL database. ' . $pe->getMessage() , E_USER_ERROR);
    }
}

输出将是一个数组:

Array (
[0] => id
[1] => name
[2] => email
[3] => shoe_size
[4] => likes
... )

对不起死灵,但我喜欢我的功能;)

附注:我没有包括类核心,但你可以使用自己的类。科学博士

我写了一个简单的php脚本,通过php获取表列: Show_table_columns.php

<?php
$db = 'Database'; //Database name
$host = 'Database_host'; //Hostname or Server ip
$user = 'USER'; //Database user
$pass = 'Password'; //Database user password
$con = mysql_connect($host, $user, $pass);
if ($con) {
    $link = mysql_select_db($db) or die("no database") . mysql_error();
    $count = 0;
    if ($link) {
        $sql = "
            SELECT column_name
            FROM   information_schema.columns
            WHERE  table_schema = '$db'
                   AND table_name = 'table_name'"; // Change the table_name your own table name
        $result = mysql_query($sql, $con);
        if (mysql_query($sql, $con)) {
            echo $sql . "<br> <br>";
            while ($row = mysql_fetch_row($result)) {
                echo "COLUMN " . ++$count . ": {$row[0]}<br>";
                $table_name = $row[0];
            }
            echo "<br>Total No. of COLUMNS: " . $count;
        } else {
            echo "Error in query.";
        }
    } else {
        echo "Database not found.";
    }
} else {
    echo "Connection Failed.";
}
?>

享受吧!