Problems with new fields in PHP mysql

Database table table
New field json

how can I tell if the json field exists? If it does not exist, create a field json in varchar format

because the code is common to many programs. Some databases already have the json field, and some do not.. How to judge when inserting.

Mar.16,2021

there is a COLUMNS table in MySQL's information_schema library that records the field information of all tables in all mysql libraries. So check whether there is a json field in each table directly according to the information of this table, and then create it.

IF NOT EXISTS( SELECT NULL
            FROM INFORMATION_SCHEMA.COLUMNS
           WHERE table_name = 'tablename'
             AND table_schema = 'db_name'
             AND column_name = 'json')  THEN
  ALTER TABLE `TableName` ADD `json` VARCHAR(255) NOT NULL;
END IF;

IF NOT EXISTS(SELECT 1 FROM  information_schema.COLUMNS WHERE TABLE_SCHEMA='()' AND table_name='table' AND COLUMN_NAME='json') THEN
     ALTER TABLE `table` ADD `json` VARCHAR(255) NOT NULL;
END IF;
Menu