browser lang:en
Questo piccolo script serve per convertire un intero Database MySql in una serie di files XML.
In pratica viene generato un file xml per ogni tabella corrispondente nel Database, ad esempio nel caso avessimo una tabella "amici" con campi "id" e "titolo", la struttura dell'xml generato sarebbe questa:
<?xml version="1.0"?>
<amici>
<record>
<id>1</id>
<titolo><![CDATA[ Pippo ]]></titolo>
</record>
<record>
<id>2</id>
<titolo><![CDATA[ Pluto ]]></titolo>
</record>
....
</amici>
$hostname = "hostname";
$database = "database";
$username = "username";
$password = "password";
// Directory where we write the xml files
$directory = "/xml/";
$cnConnection = mysql_pconnect($hostname, $username, $password);
//Conn control
if (!$cnConnection) {
print 'Could not connect to mysql';
exit;
}
//Read all tables
$result = mysql_list_tables($database);
if (!$result) {
print "DB Error, could not list tables\n";
print 'MySQL Error: ' . mysql_error();
exit;
}
//Start print all tables
while ($row = mysql_fetch_row($result)) {
$fields = mysql_list_fields($database, $row[0], $cnConnection);
$columns = mysql_num_fields($fields);
for ($i = 0; $i < $columns; $i++) {
$colonnaTabella[] = mysql_field_name($fields, $i);
$tipoColonna[] = mysql_field_type($fields, $i);
}
// CONNECT TO DATABASE
mysql_select_db($database, $cnConnection);
$query_rsRecordset = "SELECT * FROM ".$row[0];
$rsRecordset = mysql_query($query_rsRecordset, $cnConnection) or die(mysql_error());
$row_rsRecordset = mysql_fetch_assoc($rsRecordset);
$totalRows_rsRecordset = mysql_num_rows($rsRecordset);
// START STORING DATA IN VARIABLE TO PLACE IN XML FILE
if($totalRows_rsRecordset > 0) {
$strXML = "<?xml version=\"1.0\"?>\n";
// STORE NAME OF TABLE
$strXML = $strXML . " <".$row[0].">\n";
// STORE FIELD AND FIELD DATA IN ONE HIARCHY, REPEAT FOR MULTIPLE FIELDS
do {
$strXML = $strXML . " <record>\n";
foreach ($colonnaTabella as $key => $value) {
//Select the type of column and relative results
$kindOfColumn = $tipoColonna[$key];
switch ($kindOfColumn){
case 'string':
$strXML = $strXML." <".$value."><![CDATA[".$row_rsRecordset[$value]."]]></".$value.">\n";
break;
case 'blob':
$strXML = $strXML." <".$value."><![CDATA[".$row_rsRecordset[$value]."]]></".$value.">\n";
break;
default:
$strXML = $strXML." <".$value.">".$row_rsRecordset[$value]."</".$value.">\n";
}
}
$strXML = $strXML . " </record>\n";
} while ($row_rsRecordset = mysql_fetch_assoc($rsRecordset));
$strXML = $strXML . " </".$row[0].">";
// OPEN FILE, WRITE TO FILE, CLOSE FILE, CLOSE RECORDSET
$XMLFile = fopen($_SERVER['DOCUMENT_ROOT'].$directory.$row[0]."_xml.xml", "w") or die("can't open file");
fwrite($XMLFile, $strXML);
fclose($XMLFile);
}
mysql_free_result($rsRecordset);
//Empty the arrays
$colonnaTabella = array();
$tipoColonna = array();
}
mysql_free_result($result);
echo 'Db Processed...';
Per i piú smemorati, una lista di comandi principali usati con linux cd : cambia la directory corrente. ls : mostra il…
in:Scripts e tutorials (0 commenti)Con l'avvento di caffeine molti webmaster si sono trovati a dover rivedere tutte le strategie SEO per i…
in:Scripts e tutorials (0 commenti)Questo è un widget di jQuery per il nuovo social network di Google-Buzz-che è possibile incorporare ovunque per…
in:Scripts e tutorials (0 commenti)Questo é un pratico codificatore/decodificatore/decriptatore (chiamatelo come volete) di password inserite nei file di configurazione .ste su dreamweaver function…
in:Scripts e tutorials (0 commenti)Il PageRank è un algoritmo che usa Google per valutare la credibilità e l'autorevolezza delle pagine Web. Il…
in:Scripts e tutorials (8 commenti)Con questo script possiamo limitare il download di un file dal nostro server php; il limite di velocitá…
in:Scripts e tutorials (0 commenti)Alcuni host hanno disabilitati nei settaggi del.ini i comandi allow_url_fopen. Questo significa anche non poter usare i vantaggi…
in:Scripts e tutorials (2 commenti)Gli SHORT URLS fanno ormai parte di quelle cose di internet che ci suonono terribilmente familiari; per capirci…
in:Scripts e tutorials (0 commenti)
