如何在php/mysql中获取结果集及其列名?

14

这样可以吗?

$i = 0;
while ($row = mysql_fetch_array($result))
{
    $resultset[] = $row;
    $columns[] = mysql_fetch_field($result, $i);
}

然后尝试打印

<tr><th><?php echo $columns[0] ?></th><th><?php echo $columns[1] ?></th></tr>

我收到了一个错误

Catchable fatal error: Object of class stdClass could not be converted to string

我注意到$i没有被递增,始终保持为0。此外,在这个例子中,$row和$resultset没有被使用,而$result也没有在示例的范围内定义。 - Ewan Todd
1
你说得对,我忘记增加 $i 的值了 :) - programmernovice
5个回答

26

尝试使用mysql_fetch_field函数。

例如:

<?php
$dbLink = mysql_connect('localhost', 'usr', 'pwd');
mysql_select_db('test', $dbLink);

$sql = "SELECT * FROM cartable";
$result = mysql_query($sql) or die(mysql_error());

// Print the column names as the headers of a table
echo "<table><tr>";
for($i = 0; $i < mysql_num_fields($result); $i++) {
    $field_info = mysql_fetch_field($result, $i);
    echo "<th>{$field_info->name}</th>";
}

// Print the data
while($row = mysql_fetch_row($result)) {
    echo "<tr>";
    foreach($row as $_column) {
        echo "<td>{$_column}</td>";
    }
    echo "</tr>";
}

echo "</table>";
?>

16

使用mysql_fetch_assoc函数获取仅为关联数组,并在第一次循环中检索列名:

$columns = array();
$resultset = array();
while ($row = mysql_fetch_assoc($result)) {
    if (empty($columns)) {
        $columns = array_keys($row);
    }
    $resultset[] = $row;
}

现在你可以在第一次迭代时打印出你的表格头部:

echo '<table>';
$columns = array();
$resultset = array();
while ($row = mysql_fetch_assoc($result)) {
    if (empty($columns)) {
        $columns = array_keys($row);
        echo '<tr><th>'.implode('</th><th>', $columns).'</th></tr>';
    }
    $resultset[] = $row;
    echo '<tr><td>'.implode('</td><td>', $rows).'</td></tr>';
}
echo '</table>';

1
你也可以在循环外删除if并执行$columns = isset($resultset[0])? array_keys($resultset[0]): array(); :) - Carlos Lima
嗨,谢谢,好主意,比使用mysql_fetch_field字段更好,但我投票给下面的帖子,因为它是对我的问题的直接回答 :) - programmernovice
即使过了10多年,implode仍然是一个不错的解决方案。 - Timo

8
你想查看的是:
 mysql_fetch_assoc

该函数将每一行作为关联键=>值对返回,其中键是列名。

文档在这里


1
这是一个更现代的解决方案,使用...i
$db_link = @mysqli_connect($server, $user, $pass, $db) or die("Connection failed");
$spalten = ['pc_name', 'pc_type', 'pc_snr', 'pc_bj', 'mo_port_1', 'mo_snr_1', 'mo_bj_1', 'mo_port_2', 'mo_snr_2', 'mo_bj_2'];

$abfrage = "SELECT " . implode(',', $spalten) . " FROM hardware order by pc_name";

$liste = mysqli_query($db_link, $abfrage);
$spalten = mysqli_num_fields($liste);


while ($werte[] = mysqli_fetch_array($liste, MYSQLI_ASSOC)) {}
    <table class="display" id="table" style="width:100%">
        <thead>
            <tr>
                <?php
                for ($i = 0; $i < $spalten; $i++) {
                    $feldname[$i] = mysqli_fetch_field_direct($liste, $i)->name;
                    echo '<th>' . ucfirst($feldname[$i]) . '</th>';
                }
                ?>
            </tr>
        </thead>
        <tbody>
            <?php
            for ($i = 0; $i < mysqli_num_rows($liste); $i++) {
            ?>
                <tr>
                    <?php for ($j = 0; $j < $spalten; $j++) {
                    ?>
                        <td><?php
                            echo $werte[$i][$feldname[$j]];
                            ?>

                        </td>
                    <?php } ?>
                </tr>
            <?php } ?>
        </tbody>
    </table>

0

虽然它已经被弃用并且在PHP 7中不再使用,但您可以通过使用函数mysql_field_name来避免使用对象,该函数返回一个字符串。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接