Re: Dynamic Data

From: Date: Mon, 07 Jun 2004 18:24:17 +0000
Subject: Re: Dynamic Data
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-34674@lists.php.net to get a copy of this message
Christopher J. Crane wrote:
What is the best way to produce a report listing the fieldname and then the data in that field. It is a report containing only one row from a table, but I don't want to hard code the fields since they change often. I can get the field names dynamically like this: $fields = mysql_list_fields("Network", "Subnets"); $num_columns = mysql_num_fields($fields); for($ i = 0; $i < $num_columns; $i++) { echo mysql_field_name($fields, $i); } now is where I get confused....what I would like to happen is do a query on a single row and put the results into mysql_fetch_assoc and during the prior loop put the column name into the loop of the data. Something like: $fields = mysql_list_fields("Network", "Subnets"); $num_columns = mysql_num_fields($fields); $result = mysql_query("SELECT * FROM table1 WHERE ID = '$ID'; $field = mysql_fetch_assoc($result) for($ i = 0; $i < $num_columns; $i++) {
    echo "<b>" . mysql_field_name($fields, $i) . ": </b>" .
$field[mysql_field_name($fields, $i)] . "<br>\n";
All you really need here is: $sth = mysql_query("SELECT * FROM table1 WHERE ID = '$ID'"); $rec = mysql_fetch_assoc($sth); foreach($rec as $field => $value) { echo '<b>'.$field.': </b>'.$value.'<br/>'; } -- paperCrane <Justin Patrin>

« previous php.db (#34674) next »