Jump to content

Dynamic HTML Table from MySql query problem *SOLVED*


cyprus

Recommended Posts

Can someone spot why this is not working properley. Its supposed to dynamically create an HTML table from a database query. But the columns are getting repeated, and the captions are misplaced. Many thanks

[code]<?php
if (($result)||(mysql_errno == 0))
{

echo "<table border='1' bordercolor='#000000' bgcolor='#FFFFCC' width='80%'><tr>";

  if (mysql_num_rows($result)>0)
  {
          //loop thru the field names to print the correct headers
          $i = 0;
          while ($i < mysql_num_fields($result))
          {
      echo "<td align='left'>". mysql_field_name($result, $i) . "</td>";
      //
$i++;
    }
    echo "</tr>";
   
    //display the data
    while ($rows = mysql_fetch_array($result))
    {
      echo "<tr>";
      foreach ($rows as $data)
      {
        echo "<td align='center'>". $data . "</td>";
      }
    }
  }else{
    echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>";
  }
  echo "</table>";
}else{
  echo "Error in running query :". mysql_error();
}
?>
[/code]

MOD EDIT code tags added
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.