Jump to content

show fields from table and php


russthebarber

Recommended Posts

Here is the last thing I tried. i'm getting closer but need to know what to put in place of xxxx in the while loop to get the field name.

 

$sql  = "SHOW FIELDS FROM apartmentoUsers";
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result)) {
echo xxxxx;//what goes here?
}

Link to comment
Share on other sites

Have you executed this query from within MySql to see what it might look like? Your query will return something like:

 

[pre]

+------------------+---------------------+------+-----+---------------------+----------------+

| Field            | Type                | Null | Key | Default            | Extra          |

+------------------+---------------------+------+-----+---------------------+----------------+

| id          | bigint(20) unsigned | NO  | PRI | NULL                | auto_increment |

| somefield        | varchar(255)        | NO  |    |                    |                |

+------------------+---------------------+------+-----+---------------------+----------------+

[/pre]

 

Armed with this information you can see to use:

 

$sql  = "SHOW FIELDS FROM apartmentoUsers";
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    while ($row = mysql_fetch_assoc($result)) {
      echo $row['Filed'] . '<br />';
      echo $row['Type'] . '<br />';
      echo $row['Null'] . '<br />';
      echo $row['Key'] . '<br />';
      echo $row['Default'] . '<br />';
      echo $row['Extra'] . '<br />';
    }
  }
}

 

Of course, $row is just an array, so you could also have looped through it also.

 

$sql  = "SHOW FIELDS FROM apartmentoUsers";
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    while ($row = mysql_fetch_assoc($result)) {
      foreach ($row as $val) {
        echo $val . '<br />';
      }
    }
  }
}

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.