/ Published in: PHP
Export entire database schema to local xml file.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
private function execute( $sql ) { return $this->mysqli->query ( $sql ); } /** * I save the entire database schema to a local xml file. * * @param [string] $database the name of your database * @return [result] true or false */ public function saveSchemaXML( $database ) { $dom = new DOMDocument ( '1.0' ); /************************************ * Builds the root ************************************/ //create a element $schema = $dom->createElement ( 'schema' ); //set the element on itself $schema = $dom->appendChild ( $schema ); //set a attribute for the schema node $schema->setAttribute ( 'name', $database ); /*********************************** * Builds the table inside the root **********************************/ $tableQuery = $this->execute ( "SHOW TABLES FROM $database" ); { //create a element $table = $dom->createElement ( 'table' ); //set the element on itself $table = $dom->appendChild ( $table ); //set a attribute $table->setAttribute ( 'name', $tableRow [ 0 ] ); $fieldQuery = $this->execute ( "DESCRIBE $database.$tableRow[0]" ); { /*********************************** * Builds the attributes inside the table **********************************/ //create a element $field = $dom->createElement ( 'field' ); //set the element on itself $field = $dom->appendChild ( $field ); //set the name attribute $field->setAttribute ( 'name', $fieldRow [ 'Field' ] ); //set the type attribute $field->setAttribute ( 'type', $this->replaceNumbers ( $fieldRow [ 'Type' ] ) ); //set the null attribute if ( $fieldRow [ 'Default' ] != '' ) { //set the default } if ( $fieldRow [ 'Key' ] != '' ) { //set the key } if ( $fieldRow [ 'Extra' ] != '' ) { //set the value/length attribute } //put the field inside of the table $table->appendChild ( $field ); } //put the table inside of the schema $schema->appendChild ( $table ); } $dom->formatOutput = true; $dom->saveXML (); //save the file $xml = $dom->save ( $filename ); //change the permissions return $xml; }