Okey ich habe mich jetzt doch noch dran gemacht ein Script zu schreiben.
Die Ausgabe soll jener entsprechen, die phpmyadmin macht, wenn man eine Datenbank exportiert.
[code]include(“dbconnect.php”);
/* Backup erstellen */
$table = ‘guestbook’; //Name der zusichernden Tabelle
$backupFile = ‘gbbackup.sql’; //Pfad, wo soll Datei gespeichert werden?
$sql = “SELECT * FROM $table”;
$result = mysql_query($sql);
$fields = mysql_num_fields($result);
$rows = mysql_num_rows($result);
//oberer Teil
$string = 'SET SQL_MODE=“NO_AUTO_VALUE_ON_ZERO”;
–
– Datenbank: ???
–
– Tabellenstruktur für Tabelle '.$table.'
CREATE TABLE IF NOT EXISTS '.$table."
(\n";
for ($i=0; $i < $fields; $i++) {
$name = mysql_field_name($result, $i);
$type = mysql_field_type($result, $i);
$len = mysql_field_len($result, $i);
$flags = mysql_field_flags($result, $i);
$string .= " $name
$type($len) $flags";
$string .= ($i+1 == $fields)?"\n":",\n";
}
$string .= ') ENGINE=MyISAM DEFAULT CHARSET=latin1 ;
–
– Daten für Tabelle guestbook
INSERT INTO '.$table."
(";
for ($i=0; $i < $fields; $i++) {
$name = mysql_field_name($result, $i);
$string .= “$name
”;
$string .= ($i+1 == $fields)?"":", ";
}
$string .= “) VALUES’;\n”;
//unterer Teil
$j = 1;
while($row = mysql_fetch_assoc($result)){
$string .= ‘(’;
for($i=0;$i<$fields;$i++){
$spalte = mysql_field_name($result, $i);
$row[$spalte] = str_replace("\r", “\r”, $row[$spalte]);
$row[$spalte] = str_replace("\n", “\n”, $row[$spalte]);
$string .= “’$row[$spalte]’”;
$string .= ($i+1 == $fields)?"":", “;
}
$string .= ($j == $rows)?”);\n":"),\n";
$j++;
}
//In die Datei schreiben
$fp = fopen($backupFile,“w”);
fwrite($fp, $string);
fclose($fp);[/code]
Der INSERT Teil funktioniert wunderbar!
Nur mit dem Create Teil hab ich schwirigkeiten.
In phpmyadmin sieht das so aus:
CREATE TABLE IF NOT EXISTS `guestbook` (
`id` int(100) NOT NULL auto_increment,
`datum` datetime NOT NULL,
`name` varchar(70) NOT NULL,
`mail` varchar(70) default NULL,
`url` varchar(100) default NULL,
`text` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;
bei mir so:CREATE TABLE IF NOT EXISTS `guestbook` (
`id` int(100) not_null primary_key auto_increment,
`datum` datetime(19) not_null binary,
`name` string(70) not_null,
`mail` string(70) ,
`url` string(100) ,
`text` blob(65535) not_null blob
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;
einiges kann ich durch str_replace ersetzen (zum beispiel das not_null zu NOT NULL wird).
Aber wieso werden die feldtypen falsch angezeigt?
Und wie bekomme ich den primary key raus, bzw. wie ist die systax dafür?
Oder hat jemand ne bessere idee, als diese Befehle?
$name = mysql_field_name($result, $i);
$type = mysql_field_type($result, $i);
$len = mysql_field_len($result, $i);
$flags = mysql_field_flags($result, $i);