<?php
mysql_connect(‘localhost:3307′,’root’,’slopoc’); /* set with your mysql database configuration */
$the_schema = ‘mhs_job_scheduler’; /* set with your database schema */
function get_table_rec($schema){
/* low query
mysql_select_db(‘information_schema’);
$query = “select distinct table_name from table_constraints where constraint_schema = ‘”.$schema.’” AND constraint_type = ‘FOREIGN KEY’ “;
*/
mysql_select_db($schema);
$query = “show tables”;
return mysql_query($query);
}
echo “<table border=1> <tr> <td colspan=3> FK DDL Regeneration of <b> $the_schema </b> Schema </td> </tr>”;
$res_table_fk = get_table_rec($the_schema);
$i = 1;
while ($rec_table_fk = mysql_fetch_array($res_table_fk)){
$table_fk = $the_schema.’.’.$rec_table_fk[0];
echo “<tr> <td> <b> $i </b> </td> <td colspan=2> <b> $table_fk </td> </td> </tr>”;
$query = “show create table $table_fk”;
$res = mysql_query($query);
while ($rec = mysql_fetch_array($res)){
echo “<tr> <td> </td> <td> DDL of $rec[0] Table </td> <td> $rec[1] </td> </tr>”;
$pos = strpos($rec[1],’FOREIGN KEY’);
if ($pos > 0){
$left = substr($rec[1],0,$pos);
$right = substr($rec[1],$pos,strlen($rec[1])-$pos);
$left_rev = strrev($left);
$pos_cons = strpos($left_rev,’TNIARTSNOC’);
$pos_coma = strpos($left_rev,’,');
$cons = substr($left,$pos-$pos_coma,$pos_coma);
$ddl_cons = $cons .’ ‘.substr($right,0,strlen($right)-(strlen($right)-strpos($right,’ENGINE’)));
$ddl_cons = ‘ALTER TABLE `’.$table_fk.’` ADD ‘.$ddl_cons.’;';
echo “<tr> <td> </td> <td> FK DDL </td> <td> <font color=BLUE> $ddl_cons </font> </td> </tr>”;
} else {
echo “<tr> <td> </td> <td> FK DDL </td> <td> <font color=RED> NO FK </font> </td> </tr>”;
}
}
$i++;
}
echo “</table>”;
?>
download disini



























