Decoded from a JSON object I would like to insert a associative array to MySQL via PHP.
I found several examples already like https://www.daniweb.com/web-development/php/threads/381669/json-to-mysql-with-php that handle one-time imports like that - also with unknown keys.
But in my case I need to do inserts (from this associative array) again and again to the same table (without droping and recreating this table first) and there may appear new keys in the array anytime which also need to be reflected as new columns (all defined as "varchar(255)") in the MySQL table.
So basically before doing the insert the existent columns need to be checked and supplemented with the new columns if needed.
I searched a while now, but didn't find anyone who did the same before and just can't believe that I'm the first one who needs this. :-)
Does anyone know an example regarding that coincidentally?
Thanks in advance!
SHOW COLUMNS, although that is MySQL-specific so not portable. For creating the columns, the db user that the php script uses will need theALTERprivilege, and then you would create the column usingALTER TABLE. I'm not sure what your site/script is used for, but that would entail giving a lot of access to that user, which may not be desirable.id|key|valuetable, which properly represents such variant structured data sets? (That's just negligibly more work accessing it then). Or wouldn't it fit better to store extraneous/rare/non-standard fields just on a JSON blob alongside a standarized column scheme? (Also easy to recombine in application logic).