Jump to content

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

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.