2009-12-05 8 views
10

OK?Come ottenere i nomi delle colonne insieme a resultset in php/mysql?

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

Poi, quando si tenta di stampare

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

ho ottenuto un errore

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

Ho notato che $ i non viene incrementato, avendo sempre il valore 0. Inoltre, '$ row' e' $ resultset' sono inutilizzati in questo esempio e '$ risultato' non è definito nell'ambito di questo esempio. –

+0

Hai ragione hai dimenticato di incrementare $ i :) – programmernovice

risposta

19

Prova la funzione mysql_fetch_field.

Ad esempio:

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

@Atli: Bella foto! :) –

+0

Grazie. Di nuovo a 'ya! :) – Atli

8

si vuole guardare al

mysql_fetch_assoc 

che fornisce ad ogni riga come una chiave associativa => coppia di valori in cui la chiave è il nome della colonna.

Documentazione here

12

Usa mysql_fetch_assoc di ottenere solo un array associativo e recuperare i nomi delle colonne con la prima iterazione:

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

Ora è possibile stampare la testa del vostro tavolo con il prima iterazione pure:

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

Puoi anche rimuovere if e do $ columns = isset ($ resultset [0])? array_keys ($ resultset [0]): array(); fuori dal giro :) –

+0

Ciao grazie, ottima idea, meglio che usare il campo mysql_fetch_field ma ho votato per il post qui sotto in quanto è la risposta diretta alla mia domanda :) – programmernovice

0

Anche se è deprecato e non più in PHP 7 puoi evitare di dover usare un oggetto usando la funzione mysql_field_name che restituisce una stringa.

0

Prova questo

 $result=mysql_query('SELECT * FROM `table1` where id=5') or die ('query failed'); 
echo "<table>"; 
     while ($row=mysql_fetch_assoc($result)) { 
       $column = array_keys($row); 
       for($i=0; $i<sizeof($column); $i++){ 
       echo "<tr><th>".$column[$i]."</th><td>".$row[$column[$i]]."</td></tr>"; 
       } 
      } 
echo "</table>"; 

O

$result=mysql_query('SELECT * FROM `table1` where id=5') or die ('query failed'); 
    echo "<table>"; 
      $row=mysql_fetch_assoc($result); 
        $column = array_keys($row); 
        for($i=0; $i<sizeof($column); $i++){ 
        echo "<tr><th>".$column[$i]."</th><td>".$row[$column[$i]]."</td></tr>"; 
        } 

    echo "</table>"; 
Problemi correlati