Create MYSQL TABLE using PHP ARRAY

As a web developer I hate when I have to create mysql table during middle of the project. Most of the clients do not provide complete project scope due to that some of the scenarios and scope remain uncovered during database design.

In my recent project I have faced this type of issue. At the eleventh hour client came to me and said please create another view for CSV upload and link csv to another module. First I really get angry but you know CLIENT IS ALWAYS RIGHT.


One another thing he told me that CSV has 217 fields. Yes! Only 217 fields which means I have to create 217 columns in mysql table. I could not use JSON data type for this because if I would, then i had to alter my most of the code which I couldn’t.

I came up with an idea, Why not create an associative array using first row (CSV Column) and second row (for column values) and then write a PHP code for Mysql table and execute that. So i do not need to create mysql table manually.

Idea works!

So in this post I am going to share that code with you. No no do not panic i will not list all 217 fields 🙂

Convert PHP Array to Mysql Table

Output:

Just execute above code in the browser and table code will print. Copy and paste that code to phpmyadmin or anyother mysql client. Or if you want to execute create table statement so you need to create database connection and pass $createTableStatement variable mysqli_query()

Also read:

 

Posted in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *