user3223205
user3223205

Reputation:

PHP echo mysql field names in while loop

I am running this Select query:

$sql="SELECT * from callplandata group by description ";
$rs=mysql_query($sql,$conn);
while($row = mysql_fetch_row($rs)) {
    echo '<tr>';
    for($i=0; $i<$fields_num; $i++) {
                $field = mysql_fetch_field($rs);
        echo '<td>
        <input type="text" name="columnname'.$x.'" value="'.$field->name.'" />
        <input type="text" name="" value="'.$row[$i].'" />
        </td>';
    }
    echo '</tr>';
}

to display mysql table data and column names.

i use $field->name to show the column name but it is not showing any column names

Upvotes: 1

Views: 1162

Answers (2)

senK
senK

Reputation: 2802

you need to use the index too

echo '<tr>';
for($i=0; $i<$fields_num; $i++) {
     $field = mysql_fetch_field($rs,$i);//use the index value here
    echo '<td>
    <input type="text" name="columnname'.$x.'" value="'.$field->name.'" />
    <input type="text" name="" value="'.$row[$i].'" />
    </td>';
}

Upvotes: 0

hsz
hsz

Reputation: 152226

Instead of mysql_fetch_row use mysqli_fetch_assoc, then just loop over the row:

$rs = mysqli_query($sql,$conn);
while ($row = mysql_fetch_assoc($rs)) {
  foreach ($row as $column => $value) {

  }
}

Upvotes: 2

Related Questions