Jump to content

[SOLVED] Question on displaying mysql_query result variable


abdfahim

Recommended Posts

Let consider the following code

$query="SELECT * FROM table";
$result=mysql_query($query);

Now we all know how to display the results. But consider I have only $result variable and I dont have any idea of the mysql table - how many field, field's name etc. - I know nothing. Even its not necessary that $query always contains all fields * (then I could use SHOW FIELDS command), may be some times it has selected fields like

$query="SELECT (a,b,c,d,e) FROM table";

But I don't know which fields is included in Query.

 

Then how can I display the Query Results with field names?

Link to comment
Share on other sites

<?php

  $query = "SELECT * FROM table";
  if ($result = mysql_query($query)) {
    if (mysql_num_rows($result)) {
      while ($row = mysql_fetch_assoc($result)) {
        foreach ($row as $key => $value) {
          echo "$key = $value<br />";
        }
      }
    }
  }

?>

Link to comment
Share on other sites

Now this is cool. I didn't know that. thanx .... It shows each cell in a new line like

field1 = abc
field2 = def
field3 = geh
field1 = ijk
field2 = lmn
field3 = opq

So if I have to show this in tabular form like

field1      field2     field3
abc         def        geh
ijk           lmn        opq

should I need to use php functions (like explode to get field name and compare string to get when the next row start) or there is any straight forward proccess?

Link to comment
Share on other sites

Something like this?

<?php
  $count = 0;
  echo "<table><tr><th>field1</th><th>field2</th><th>field3</th></tr>";
  $query = "SELECT * FROM table";
  if ($result = mysql_query($query)) {
    if (mysql_num_rows($result)) {
      while ($row = mysql_fetch_assoc($result)) {
        foreach ($row as $key => $value) {
          if($count!=0&&$count%3==0) echo "</tr><tr>";
          echo "<td>$value</td>";
        }
      }
    }
  }
  echo "</tr></table>";
?>

Link to comment
Share on other sites

Something like....

 

<?php

  $header = false;
  echo "<table>"
  $query = "SELECT * FROM table";
  if ($result = mysql_query($query)) {
    if (mysql_num_rows($result)) {
      while ($row = mysql_fetch_assoc($result)) {
        if (!$header) {
          $fields = array_keys($row);
          echo " <tr>";
          foreach($fields as $field) {
            echo "  <td>$field</td>";
          }
          echo " </tr>";
          $header = true;
        }
        echo " <tr>";
        foreach ($row as $value) {
          echo "  <td>$value</td>";
        }
        echo " </tr>";
      }
    }
  }
  echo "</table>";

?>

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.