Mega Code Archive

 
Categories / Php / MySQL Database
 

Initializing the MSQL-tables via a 2-dimens array

A good way of organizing/controling the initializingstage is to use a 2- dimensional array filled with your SQL-statementstrings. Each category has its own array. Now you can loop through the categories , execute the statements and see whether they succeeded or not. It can be done this way: <? $Dbname = Firstbase; $First_table = Person; $Sec_table = Action; $Third_table= Users; $Autoid_table = Autoid; $actions = array ( /* DROP TABLES */ // Delete all the tables. This also // deletes all the indices and sequences. array( "DROP TABLE $First_table", "DROP TABLE $Sec_table", "DROP TABLE $Third_table", "DROP TABLE $Autoid_table" ), /* CREATE TABLES */ array ( "create table $First_table(UID int,F_name char(17),L_name char(20)". ", Birth date,Phone char(10), Email char(25))", "create table $Sec_table(Idnr int,Dow char(10),Date_out date". ",Time_out time,Datetime_in char(28))", "create table $Third_table(id char(25),pw char(25))", "create table $Autoid_table(Id_num int)" ), /*CREATE INDICES & SEQUENCE */ array( "CREATE UNIQUE INDEX index1 ON $First_table(UID)", "CREATE INDEX index2 ON $First_table(F_name)", "CREATE INDEX index3 ON $First_table(L_name)", "CREATE UNIQUE INDEX index4 ON $Sec_table(Idnr)", "CREATE INDEX index5 ON $Sec_table(Date_out)", "CREATE INDEX index6 ON $Third_table(id,pw)", "create sequence on $Autoid_table step 1 value 0" ) ); // end of 2-dimensional array /* EXECUTE QUERIES */ for ($counter = 0; $counter <= 2;$counter++) { $number = count($actions[$counter]); // count number of array-elements for ($teller = 0; $teller < $number ;$teller++) { echo $actions[$counter][$teller]; $result = msql($Dbname, $actions[$counter][$teller]); if ($result > 0){ ECHO " = Succesfull.<br>"; } else { ECHO " = Unsuccesfull.<br>"; } } } ?>