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
https://forums.phpfreaks.com/topic/72762-displaying-table-field-list/
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); 


						 }

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.

I want to use it so I don't have to type out the table fields everytime I want to select something from it.  So I figure that it would be easier to use a function to list them.  Then I would only have to use SELECT $tablefields FROM news to select information from a news table.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.