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

后端 未结 5 1145
生来不讨喜
生来不讨喜 2020-12-06 05:56

Is this OK ?

$i = 0;
while ($row = mysql_fetch_array($result))
{
    $resultset[] = $row;
    $columns[] = mysql_fetch_field($result, $i);
}
<
相关标签:
5条回答
  • 2020-12-06 06:11

    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>
    
    0 讨论(0)
  • 2020-12-06 06:20

    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

    0 讨论(0)
  • 2020-12-06 06:21

    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>";
    ?>
    
    0 讨论(0)
  • 2020-12-06 06:23

    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>';
    
    0 讨论(0)
  • 2020-12-06 06:25

    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 讨论(0)
提交回复
热议问题