Jump to content

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.

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.