mercredi 5 mai 2021

mysql IF column EXISTS THEN delete record from table

I need to delete a record from a table if columnID=X, but only from tables that have columnID.

$query = "show tables" ;
list($selCount,$tables,$selError) = dbSelect($query,array()) ;
foreach ($tables as $table) {
  $tableName = $table['Tables_in_dbName'] ;
  $query = "IF EXISTS (
        SELECT * FROM information_schema.columns 
            WHERE table_name = '$tableName' 
               AND column_name='cID' 
               AND table_schema= DATABASE()
        ) THEN DELETE FROM $tableName WHERE cID=?;
        END IF;" ;
  list($delCount,$delError) = dbDelete($query,array($corpID)) ;
}

Something is wrong with the IF EXISTS...THEN portion of my query but I am not understanding what. I have read many other examples of IF EXISTS and can't see what I am doing wrong.

Aucun commentaire:

Enregistrer un commentaire