开发者

How to get the columns names along with resultset in php/mysql?

开发者 https://www.devze.com 2022-12-13 12:55 出处:网络
Is this OK ? $i = 0; while ($row = mysql_fetch_array($result)开发者_StackOverflow) { $resultset[] = $row;

Is this OK ?

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

Then when trying to print

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

I got an error

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


Try the mysql_fetch_field function.

For example:

<?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>";
?>


Use mysql_fetch_assoc to get only an associative array and retrieve the column names with the first iteration:

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

Now you can print the head of your table with the first iteration as well:

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>';


You want to look at

 mysql_fetch_assoc

Which gives each row as an associative key => value pair where the key is the column name.

Documentation here


Here is a more modern solution with ...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>


Though it's deprecated and no longer in PHP 7 you can avoid having to use an object by using the function mysql_field_name instead which returns a string.

0

精彩评论

暂无评论...
验证码 换一张
取 消