SketchBookGames
SketchBookGames

Reputation: 514

accessing xml (Text) from sql with php

$result = mysql_query
    ("
        SELECT *
        FROM `table`
        WHERE `id` = ".$id."
    ") or die(Err(mysql_error()));

while($row = mysql_fetch_array($result))
{
    echo '&xml='.$row['xml'];  //this works
    echo '&fileName='.$row['xml']->fileName;  //this doesn't work
    echo '&fileName='.$row['xml.fileName'];  //this doesn't work
    echo '&fileName='.$row['xml->fileName'];  //this doesn't work

i can't figure out how to access an xml child by name in php.

i think part of the problem is that the xml stored in sql is returned as Text.

i hope its just a simple syntax thing since i don't know php well.

*in flash i just force the datatype to be xml and it works great, but i need to access xml.fileName in php here.

**novice php/sql.

**much better at actionscript.

Upvotes: 1

Views: 224

Answers (2)

Cristian Radu
Cristian Radu

Reputation: 8412

You're getting the data from the DB incorrectly. I assume the field that stores your XML is 'xml', since this echo '&xml='.$row['xml'] works. In that case, save this data into a variable and then use an XML parser to go through it. If you have PHP 5, you can use SimpleXML.

So your code becomes:

while($row = mysql_fetch_array($result))
{
  $xml = new SimpleXMLElement($row['xml']);
  echo $xml->filename;
}

Upvotes: 2

SergeS
SergeS

Reputation: 11779

use simplexml_load_string( $xmlString ) , then use $xml->child

eg

$xml = simplexml_load_string( $row );
echo '&fileName='.$xml->fileName; 

Upvotes: 2

Related Questions