ShoeLace1291 Posted October 11, 2007 Share Posted October 11, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/72762-displaying-table-field-list/ Share on other sites More sharing options...
Orio Posted October 11, 2007 Share Posted October 11, 2007 Because $list is an array... Try using print_r($list) instead of echo($list). Orio. Quote Link to comment https://forums.phpfreaks.com/topic/72762-displaying-table-field-list/#findComment-366933 Share on other sites More sharing options...
ShoeLace1291 Posted October 11, 2007 Author Share Posted October 11, 2007 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); } Quote Link to comment https://forums.phpfreaks.com/topic/72762-displaying-table-field-list/#findComment-366986 Share on other sites More sharing options...
Orio Posted October 11, 2007 Share Posted October 11, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/72762-displaying-table-field-list/#findComment-366992 Share on other sites More sharing options...
ShoeLace1291 Posted October 11, 2007 Author Share Posted October 11, 2007 Thanks. Last quesition about this: would there be a way to use implode to insert a comma inbetween the fields? Quote Link to comment https://forums.phpfreaks.com/topic/72762-displaying-table-field-list/#findComment-366998 Share on other sites More sharing options...
Orio Posted October 11, 2007 Share Posted October 11, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/72762-displaying-table-field-list/#findComment-367024 Share on other sites More sharing options...
ShoeLace1291 Posted October 12, 2007 Author Share Posted October 12, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/72762-displaying-table-field-list/#findComment-367840 Share on other sites More sharing options...
Orio Posted October 12, 2007 Share Posted October 12, 2007 Have you thought of simply using: SELECT * FROM table_name This retrieves all of the info... Orio. Quote Link to comment https://forums.phpfreaks.com/topic/72762-displaying-table-field-list/#findComment-368119 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.