Jump to content

last collumn name


golfromeo

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/184853-last-collumn-name/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/184853-last-collumn-name/#findComment-975843
Share on other sites

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.