You are here

private function FrxPostgres::php_xml in Forena Reports 8

1 call to FrxPostgres::php_xml()
FrxPostgres::sqlData in src/FrxPlugin/Driver/FrxPostgres.php
Get data based on file data block in the repository.

File

src/FrxPlugin/Driver/FrxPostgres.php, line 147
Postgres specific driver that takes advantage of native XML support

Class

FrxPostgres
Class FrxPostgres

Namespace

Drupal\forena\FrxPlugin\Driver

Code

private function php_xml($sql) {
  $db = $this->db;
  $xml = new \SimpleXMLElement('<table/>');
  $rs = @pg_query($sql);
  $e = pg_last_error();
  if ($e) {
    $text = $e;
    if (!$this->block_name) {
      $short = t('%e', array(
        '%e' => $text,
      ));
    }
    else {
      $short = t('SQL Error in %b.sql', array(
        '%b' => $this->block_name,
      ));
    }
    $this
      ->app()
      ->error($short, $text);
  }
  $rownum = 0;
  if ($rs) {
    while ($row = pg_fetch_assoc($rs)) {
      $rownum++;
      $row_node = $xml
        ->addChild('row');
      $row_node['num'] = $rownum;
      foreach ($row as $key => $value) {
        $row_node
          ->addChild(strtolower($key), htmlspecialchars($value));
      }
    }
  }
  if ($rs) {
    pg_free_result($rs);
  }
  return $xml;
}