Adding columns to a MySQL table using PHP.

This is a tutorial on how to add columns to a MySQL table using PHP. Please note that we will be using the PDO object in this tutorial (although the SQL queries will remain the same, regardless of whether you’re using PDO, mysql or mysqli).

For this tutorial, I will be using the following table structure:

As you can see, the table has two columns: A Primary INT column called “id” and a VARCHAR column called “title”.

Let’s add a simple DATE column to the end of our “reports” table.

In the code above, we connected to MySQL using the PDO object before executing our ALTER TABLE SQL query. In this case, we added a DATE column called “report_date” to the end of the table.

But what if we wanted to add our column before another existing column? What if we don’t want our new column to be placed at the end of the table?

In the example above, we altered the table and added the new column AFTER the first column, which is “id”.

Hopefully, you found this guide to be helpful!

Comments

comments