Skip to content

Commit a061fa0

Browse files
author
Radovan Janjic
committed
Added: Query 2 XML
1 parent c51aa2b commit a061fa0

File tree

1 file changed

+74
-3
lines changed

1 file changed

+74
-3
lines changed

MySQL_wrapper.class.php

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private function __wakeup() {
205205
*/
206206
public function call($func) {
207207
// Functions without link parameter
208-
$l = array('free_result', 'fetch_assoc', 'num_rows', 'num_fields');
208+
$l = array('free_result', 'fetch_assoc', 'num_rows', 'num_fields', 'fetch_object', 'fetch_field_direct');
209209
// Add return value
210210
$r = array('free_result' => TRUE);
211211
// Params
@@ -638,7 +638,7 @@ public function exportTable2CSV($table, $file, $columns = '*', $where = NULL, $l
638638
unlink($file);
639639

640640
// Put columns into array if not *
641-
if($columns != '*' && !is_array($columns)){
641+
if ($columns != '*' && !is_array($columns)) {
642642
$stringColumns = $columns;
643643
$columns = array();
644644
foreach (explode(',', $stringColumns) as $c) {
@@ -649,7 +649,7 @@ public function exportTable2CSV($table, $file, $columns = '*', $where = NULL, $l
649649
// Prepare SQL for column names
650650
if ($showColumns) {
651651
$tableColumnsArr = array();
652-
if ($columns == '*'){
652+
if ($columns == '*') {
653653
foreach ($this->getColumns($table) as $c)
654654
$tableColumnsArr[] = "'{$c}' AS `{$c}`";
655655
} elseif (is_array($columns)) {
@@ -871,6 +871,77 @@ public function deleteRow($table, $where = NULL, $limit = 0) {
871871
return $this->query("DELETE FROM `{$table}`" . ($where ? " WHERE {$where}" : NULL) . ($limit ? " LIMIT {$limit}" : NULL) . ";") ? $this->affected : FALSE;
872872
}
873873

874+
/** Export query to XML file or return as XML string
875+
* @param string $query - mysql query
876+
* @param string $rootElementName - root element name
877+
* @param string $childElementName - child element name
878+
* @return string - XML
879+
*/
880+
public function query2XML($query, $rootElementName, $childElementName, $file = NULL) {
881+
// Save to file
882+
if (!empty($file)) {
883+
$fh = fopen($file, 'w') or $this->error("ERROR", "Can't create XML file: {$file}");
884+
if (!$fh) {
885+
return FALSE;
886+
}
887+
fclose($fh);
888+
$file = realpath($file);
889+
$saveToFile = TRUE;
890+
} else {
891+
$saveToFile = FALSE;
892+
}
893+
894+
// Do query
895+
$r = $this->query($query);
896+
897+
// XML header
898+
if ($saveToFile) {
899+
file_put_contents($file, "<?xml version=\"1.0\" encoding=\"" . strtoupper($this->charset) . "\" ?>\n<" . $rootElementName . ">\n", LOCK_EX);
900+
} else {
901+
$xml = "<?xml version=\"1.0\" encoding=\"" . strtoupper($this->charset) . "\" ?>\n";
902+
$xml .= "<" . $rootElementName . ">\n";
903+
}
904+
905+
// Query rows
906+
while($row = $this->call('fetch_object', $r)) {
907+
// Create the first child element
908+
$record = "\t<" . $childElementName . ">\n";
909+
for ($i = 0; $i < $this->call('num_fields', $r); $i++) {
910+
// Different methods of getting field name for mysql and mysqli
911+
if ($this->extension == 'mysql') {
912+
$fieldName = $this->call('field_name', $r, $i);
913+
} elseif ($this->extension == 'mysqli') {
914+
$colObj = $this->call('fetch_field_direct', $r, $i);
915+
$fieldName = $colObj->name;
916+
}
917+
// The child will take the name of the result column name
918+
$record .= "\t\t<" . $fieldName . ">";
919+
// Set empty columns with NULL and escape XML entities
920+
if(!empty($row->$fieldName)) {
921+
$record .= htmlspecialchars($row->$fieldName, ENT_XML1);
922+
} else {
923+
$record .= NULL;
924+
}
925+
$record .= "</" . $fieldName . ">\n";
926+
}
927+
$record .= "\t</" . $childElementName . ">\n";
928+
if ($saveToFile) {
929+
file_put_contents($file, $record, FILE_APPEND | LOCK_EX);
930+
} else {
931+
$xml .= $record;
932+
}
933+
}
934+
935+
// Output
936+
if ($saveToFile) {
937+
file_put_contents($file, "</" . $rootElementName . ">\n", FILE_APPEND | LOCK_EX);
938+
return TRUE;
939+
} else {
940+
$xml .= "</" . $rootElementName . ">\n";
941+
return $xml;
942+
}
943+
}
944+
874945
/** Begin Transaction
875946
* @param void
876947
*/

0 commit comments

Comments
 (0)