Follow these steps (you can easily implement this in PHP, I assumed the table name is Foo)
1.) Run the following code:
desc Foo
2.) Based on the results of the first step, you can create the create table command (and you should)
3.) Save the data from the existing table, which will be replaced in a variable (Optional, you only need this if you can use the data from the old table)
4.) Modify the extracted rows from step 3.) so that they are compatible with your new definition (optional, you only need this if you can use the data from the old table)
5.) Get rows from your new Foo table
6.) Combine the results obtained in steps 4.) a 5.) (Optional, you only need this if you can use the data from the old table)
7.) Run the drag table for the old table
8.) Create a replacement in the command to insert all your rows into the newly created Foo table (you can read about it here )
After these steps, you will end up with a new version of the table. If your tables are too large, you can run the CREATE TABLE IF NOT EXISTS command, and if that fails, run the alter command.
In addition, you can make a library to complete these steps and will use it in the future instead of solving the same problem several times.
EDIT: You can connect the database using this function: mysql-connect (documentation here ) You can run the query using this function: mysql-query (documentation here )
Based on the first step, you will get the names of the fields (let it be assumed that you store them in the $ bar variable), and you can use your result to generate your selection command (connecting to a database where you have important data. There may be both) :
$field_list = "1"; foreach ($bar as $key => $value) $field_list.= ",".$bar[$key]; mysql_connect(); mysql_query("select ".$field_list." from Foo");
You can use your new resource to create an insert command to insert all your important data after removing the rest (read about resources here , how you can generate your insert, you can read here , but I suggest that you replace instead its insert, which works like an insert, except that it replaces the string, if it already exists, is better here than the insert, read here )
So, use mysql_connect and mysql_query, and the resource returned by mysql_query can be used as a replacement in the future (I have now linked the URL for everything you need, so I'm sure you will solve the problem.), Apologies for were not specific enough before.