الكود غير كامل ولن يأخذ القاعدة بشكل صحيح
هذه كلاس كاملة وصحيحة بإذن الله
كود PHP:
<?php
//MySQL connection parameters
$dbhost="localhost";
$dbuser="";
$dbpsw="";
$dbname="";
//Connects to mysql server
$connessione = @mysql_connect($dbhost,$dbuser,$dbpsw);
class MySQLDump
{
function MYSQLDump($db = null, $filepath = 'dump.sql', $compress = false, $hexValue = false){
$this->compress = $compress;
if ( !$this->setOutputFile($filepath) )
return false;
return $this->setDatabase($db);
}
function getDatabaseStructure()
{
$records = @mysql_query('SHOW TABLES');
if ( @mysql_num_rows($records) == 0 )
return false;
while ( $record = @mysql_fetch_row($records) ) {
$structure .= $this->getTableStructure($record[0]);
}
return true;
}
function getTableStructure($table)
{
if ( !$this->setDatabase($this->database) )
return false;
// Structure Header
$structure = "-- \n";
$structure .= "-- Table structure for table `{$table}` \n";
$structure .= "-- \n\n";
// Dump Structure
$structure .= 'DROP TABLE IF EXISTS `'.$table.'`;'."\n";
$structure .= "CREATE TABLE `".$table."` (\n";
$records = @mysql_query('SHOW FIELDS FROM `'.$table.'`');
if ( @mysql_num_rows($records) == 0 )
return false;
while ( $record = mysql_fetch_assoc($records) ) {
$structure .= '`'.$record['Field'].'` '.$record['Type'];
if ( !empty($record['Default']) )
$structure .= ' DEFAULT \''.$record['Default'].'\'';
if ( @strcmp($record['Null'],'YES') != 0 )
$structure .= ' NOT NULL';
if ( !empty($record['Extra']) )
$structure .= ' '.$record['Extra'];
$structure .= ",\n";
}
$structure = @ereg_replace(",\n$", null, $structure);
// Save all Column Indexes
$structure .= $this->getSqlKeysTable($table);
$structure .= "\n)";
//Save table engine
$records = @mysql_query("SHOW TABLE STATUS LIKE '".$table."'");
echo $query;
if ( $record = @mysql_fetch_assoc($records) ) {
if ( !empty($record['Engine']) )
$structure .= ' ENGINE='.$record['Engine'];
if ( !empty($record['Auto_increment']) )
$structure .= ' AUTO_INCREMENT='.$record['Auto_increment'];
}
$structure .= ";\n\n-- --------------------------------------------------------\n\n";
$this->saveToFile($this->file,$structure);
}
function saveToFile($file, $data) {
if ( $this->compress )
@gzwrite($file, $data);
else
@fwrite($file, $data);
$this->isWritten = true;
}
function setDatabase($db){
$this->database = $db;
if ( !@mysql_select_db($this->database) )
return false;
return true;
}
function setOutputFile($filepath){
if ( $this->isWritten )
return false;
$this->filename = $filepath;
$this->file = $this->openFile($this->filename);
return $this->file;
}
function openFile($filename) {
$file = false;
if ( $this->compress )
$file = @gzopen($filename, "w9");
else
$file = @fopen($filename, "w");
return $file;
}
function getSqlKeysTable ($table) {
$primary = "";
unset($unique);
unset($index);
unset($fulltext);
$results = mysql_query("SHOW KEYS FROM `{$table}`");
if ( @mysql_num_rows($results) == 0 )
return false;
while($row = mysql_fetch_object($results)) {
if (($row->Key_name == 'PRIMARY') AND ($row->Index_type == 'BTREE')) {
if ( $primary == "" )
$primary = " PRIMARY KEY (`{$row->Column_name}`";
else
$primary .= ", `{$row->Column_name}`";
}
if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '0') AND ($row->Index_type == 'BTREE')) {
if ( (!is_array($unique)) OR ($unique[$row->Key_name]=="") )
$unique[$row->Key_name] = " UNIQUE KEY `{$row->Key_name}` (`{$row->Column_name}`";
else
$unique[$row->Key_name] .= ", `{$row->Column_name}`";
}
if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '1') AND ($row->Index_type == 'BTREE')) {
if ( (!is_array($index)) OR ($index[$row->Key_name]=="") )
$index[$row->Key_name] = " KEY `{$row->Key_name}` (`{$row->Column_name}`";
else
$index[$row->Key_name] .= ", `{$row->Column_name}`";
}
if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '1') AND ($row->Index_type == 'FULLTEXT')) {
if ( (!is_array($fulltext)) OR ($fulltext[$row->Key_name]=="") )
$fulltext[$row->Key_name] = " FULLTEXT `{$row->Key_name}` (`{$row->Column_name}`";
else
$fulltext[$row->Key_name] .= ", `{$row->Column_name}`";
}
}
$sqlKeyStatement = '';
// generate primary, unique, key and fulltext
if ( $primary != "" ) {
$sqlKeyStatement .= ",\n";
$primary .= ")";
$sqlKeyStatement .= $primary;
}
if (is_array($unique)) {
foreach ($unique as $keyName => $keyDef) {
$sqlKeyStatement .= ",\n";
$keyDef .= ")";
$sqlKeyStatement .= $keyDef;
}
}
if (is_array($index)) {
foreach ($index as $keyName => $keyDef) {
$sqlKeyStatement .= ",\n";
$keyDef .= ")";
$sqlKeyStatement .= $keyDef;
}
}
if (is_array($fulltext)) {
foreach ($fulltext as $keyName => $keyDef) {
$sqlKeyStatement .= ",\n";
$keyDef .= ")";
$sqlKeyStatement .= $keyDef;
}
}
return $sqlKeyStatement;
}
}
//Creates a new instance of MySQLDump: it exports a compressed and base-16 file
$dumper = new MySQLDump($dbname,'filename.sql',false,false);
$dumper->getDatabaseStructure();
?>
من موقع php classes