Jump to content

Displaying Table Field List


ShoeLace1291

Recommended Posts

I got a function that displays a list of the fields in a certain mysql table.  I got it off of this site.  The problem is that it only displays the word Array and I'm not sure why.  This is the code for the function:

 

function getfields($DB, $Table) {
				 $fldlist = mysql_list_fields($DB, $Table);
				 $columns = mysql_num_fields($fldlist);

				 for ($i = 0; $i < $columns; $i++) {
				 		 $Listing[] = mysql_field_name($fldlist, $i);
						 }
				 Return ($Listing); 
				 }

 

And this is what I'm using to call it:

$DB = "unholydesigns";
$Table = "news";

$list = getfields($DB, $Table);

echo $list;

 

Thanks for any help.

Link to comment
Share on other sites

I figured out how to display it not in an array but it only displays the first table field.  This is my code:

 $fldlist = mysql_list_fields($DB, $Table);
				 $columns = mysql_num_fields($fldlist);


				for ($i = 0; $i < $columns; $i++) {
				 		 $Listing = mysql_field_name($fldlist, $i);

						 Return ($Listing); 


						 }

Link to comment
Share on other sites

Now you've modified the script so it'll only return the last field...

 

This is the way it's supposed to be:

 

<?php

function getfields($DB, $Table) {
				 $fldlist = mysql_list_fields($DB, $Table);
				 $columns = mysql_num_fields($fldlist);

				 for ($i = 0; $i < $columns; $i++) {
				 		 $Listing[] = mysql_field_name($fldlist, $i);
						 }
				 Return ($Listing); 
				 }

$DB = "unholydesigns";
$Table = "news";

$list = getfields($DB, $Table);
foreach($list as $field)
   echo $field;

?>

 

 

Orio.

Link to comment
Share on other sites

Depends on what you wanna do. If you just want to display to the user what fields a table has, then you are right. But if you now need to use these field names to create another table or whatever, it's better to keep it in an array.

 

Orio.

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.