generate BLOB data to XML

I am trying to generate BLOB data(GIF image from MySQL) using the following code. I am getting 'An invalid character was found in text content.'. Could you please let me know if there are any suggestions?

$connection = mysql_connect( $db_host, $db_username, $db_password );

$result = mysql_query("SELECT * from files");
header("Content-Type: application/xml");

echo sqlToXml($result, "entries", "entry");

    function sqlToXml($queryResult, $rootElementName, $childElementName) {
        $w = new XMLWriter();
        $w->openMemory();
        $w->setIndent(true);
        // use tabs as indents
        $w->setIndentString("   ");
        $w->startDocument("1.0", 'UTF-8');
        $w->startElement($rootElementName);

        while($record = mysql_fetch_object($queryResult)) {
            $w->startElement($childElementName);
            for ($i = 0; $i < mysql_num_fields($queryResult); $i++) { 
                $fieldName = mysql_field_name($queryResult, $i); 
                $w->startElement($fieldName);
                if(!empty($record->$fieldName)) {
                      $w->writeCData(base64_encode($record->$fieldName));
                }
                else {
                    $w->writeCData("null");
                }
                $w->endElement(); // $fieldName
            } 


            $w->endElement();
            $w->endElement(); // $childElementName
        } 
        $w->endElement(); // $rootElementName
        $w->endDocument();
        return $w->outputMemory();
    } 

Answers


Your inner loop's pretty verbose. Why not:

while($row = mysql_fetch_assoc($result)) {
   $w->startElement($childElementName);
   foreach($row as $key => $value) {
      $w->startElement($key);
      $w->writeCData(base64_encode($value));
   }
   $w->endElement();
}

Also, note that empty() can be dangerous in cases like this, especially if your data can naturally contain things like integer 0 values. empty(0) is TRUE in PHP.


Need Your Help

(Play 2.4.2, Play Slick 1.0.0) How do I apply database Evolutions to a Slick managed database within a test?

playframework-2.4 play-slick

I would like to write database integration tests against a Play Slick managed database and apply and unapply Evolutions using the helper methods described in the Play documentation namely, Evolutions.

Same address, multiple shared_ptr counters, is it forbidden by C++ standard?

c++ c++11 shared-ptr language-lawyer

Suppose I have the need to do the following (This is just some imaginative code for discussion of the C++ standard, thus I won't discuss why I design it this way, so don't bother me with something ...