golfromeo Posted December 12, 2009 Share Posted December 12, 2009 Hello, Is there a way using a MySQL PHP function to retrieve the name of the last collumn in a table? As each collumn in the table is like this: id 1 2 3 4 5 I want to add another collumn, which will automatically be 6. However, the only way I have found out how to do this is by getting the name of the last collumn in the table (which would be the "5" collumn), incrementing it, and inserting another collumn with that incremented name. Any help is much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/184853-last-collumn-name/ Share on other sites More sharing options...
ngreenwood6 Posted December 12, 2009 Share Posted December 12, 2009 here is some code that should help you: $sql = "SELECT * FROM table"; $query = mysql_query($sql); $columns = mysql_num_fields($query); echo mysql_field_name($query,$columns-1); The columns will be the count of the fields. Then when you need to get the field name, you pass it the resource and the column that you want which in this case is the last one but since you are starting with 0 instead of 1 we subtract one...hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/184853-last-collumn-name/#findComment-975843 Share on other sites More sharing options...
golfromeo Posted December 12, 2009 Author Share Posted December 12, 2009 Thanks, that's great... Wouldn't have thought of getting it that way, thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/184853-last-collumn-name/#findComment-976108 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.