14

Is this OK ?

$i = 0;
while ($row = mysql_fetch_array($result))
{
    $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
Carlos Lima
  • 4,162
  • 29
  • 32
programmernovice
  • 3,841
  • 11
  • 43
  • 63

5 Answers5

26

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>";
?>
Atli
  • 7,855
  • 2
  • 30
  • 43
16

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>';
Gumbo
  • 643,351
  • 109
  • 780
  • 844
8

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

Doug T.
  • 64,223
  • 27
  • 138
  • 202
1

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>
Timo
  • 2,922
  • 3
  • 29
  • 28
0

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.

Tom Mulkins
  • 431
  • 4
  • 8