Potresti fare in 2 modi:
$GLOBALS['sql'] = "SHOW CREATE TABLE TUA_TABELLLA";
$GLOBALS['esegui'] = mysqli_query($GLOBALS['connessione'], $GLOBALS['sql']) or die(mysqli_error($GLOBALS['connessione']));
$GLOBALS['riga'] = mysqli_fetch_assoc($GLOBALS['esegui']);
echo $GLOBALS['riga']['Create Table'];
In tal modo esce a video il comando CREATE TABLE............
CREATE TABLE `prova` ( `id_utente` int(10) unsigned NOT NULL, ................ CONSTRAINT `FK_prova_1` FOREIGN KEY (`id_utente`) REFERENCES `codici_utenti` (`id_utente`), ..........) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC
Quindi dovresti fare una ricerca di stringa per trovare FOREIGN KEY.....
Il 2° metodo, invece, è sicuramente + efficiente. In MySQL vi è un DB chiamato information_schema dove vi sono tutte le informazioni di tutti i DB presenti.
Nel tuo caso dovresti fare:
$GLOBALS['sql'] = "SELECT COLUMN_NAME, REFERENCED_TABLE_NAME,
REFERENCED_COLUMN_NAME
FROM KEY_COLUMN_USAGE
WHERE (TABLE_SCHEMA=\"TUO_DB\" AND TABLE_NAME=\"TUA_TABELLA\"
AND REFERENCED_TABLE_NAME<>\"NULL\")";
COLUMN_NAME è il nome del campo della tua tabella
REFERENCED_TABLE_NAME è il nome della tabella a cui la chiave esterna fa riferimento
REFERENCED_COLUMN_NAME è il nome del campo tabella a cui la chiave esterna fa riferimento
Ovviamente se il nr. di righe restituito dalla query è zero, vuol dire che non vi sono chiavi esterne sulla tua tabella.

Rispondi quotando