1

我已经研究了好几天了。我总是得到这样的东西(这是我认为最好的答案):

SELECT `COLUMN_NAME` 
FROM `INFORMATION_SCHEMA`.`COLUMNS` 
WHERE `TABLE_SCHEMA`='yourdatabasename' 
AND `TABLE_NAME`='yourtablename';

是的,这个在 SQL 中完美运行,但我仍然不知道如何创建一个 PHP 脚本来以与在 SQL 中相同的方式显示它。谁能给我一个代码?

我现在真的很困惑。请帮忙..

4

3 回答 3

2

没有 PDO:

$sql = "SHOW FIELDS FROM users;";
$result_sql = mysql_query($sql) or die ("Error!\n");

    while ($row = mysql_fetch_array($result_sql)) {
        echo $row['Field']."<br>\n";
    }
于 2012-11-18T14:53:09.473 回答
1

你可以在mysql中做到这一点mysql_field_name()

不鼓励使用 ,mysql_*因此请使用pdomysqli

和他们一起你可以做到这一点

mysqli_fetch_field_direct()   //for mysqli
PDOStatement::getColumnMeta() //for pdo

例如在OP评论之后

$sql = "SELECT * from Person";
$result = mysql_query($sql,$con);

$name = mysql_field_name($result, 0);

说明如何使用mysql_field_name()

合成纤维

mysql_field_name(data,fieldOffset) 

+------------+------------------------------------------------------------------+
| data       | Required. Specifies which data pointer to use. The data          |
|            | pointer is the result from the mysql_query() function            |
+------------+------------------------------------------------------------------+
|fieldOffset |Required. Specifies which field to start returning. 0 indicates   |
|            |the first field                                                   |
+------------+------------------------------------------------------------------+

好读

MySQL 开发人员的 PDO 教程

于 2012-11-18T14:45:14.257 回答
1

显然,您需要从 mysql 表中检索数据,然后在 php.ini 中对其进行处理。有几种方法可以做到这一点,这是我最喜欢的:

<?php
    $query="SELECT `COLUMN_NAME` 
            FROM `INFORMATION_SCHEMA`.`COLUMNS` 
            WHERE `TABLE_SCHEMA`='yourdatabasename' 
            AND `TABLE_NAME`='yourtablename'";
    $result=mysql_query($query); //This will store the whole result table in $result
    $rows=array(); //Initialize array
    while($row=mysql_fetch_object($result)) $rows[]=(object) $row; //Store each row of the result table in the array $rows

    //Now you can access, for example, the third row and second column by saying:
    echo $rows[2]->name_of_second_column
?>

另请参阅本教程。关于如何从数据库中检索数据并在之后对其进行操作。

于 2012-11-18T14:51:51.153 回答