Jump to content

Recommended Posts

Can someone please tell me how to use the "SHOW FIELDS FROM $mytable" in php? I understand how the MySql works but need to get these fields back into an array in php. That is the part I am strruggling with.. I normally return rows of data from my table and use a while loop.

Link to comment
https://forums.phpfreaks.com/topic/260383-show-fields-from-table-and-php/
Share on other sites

I don't know what you mean by that. A more in depth answer would be helpful

 

while ($row = mysql_fetch_array($result)) {
$product_name = $row['product_name']; 
}

 

They are not rows so what goes in place of $row and in place of $row['product_name'] for example ???

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?
}

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 />';
      }
    }
  }
}

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.