2012-11-09 90 views
0

我以爲我正在從我的PHP腳本中正確創建一個XML文檔從一個MySQL查詢,但我沒有得到一個XML文檔作爲回報(沒有PHP錯誤來幫助我),即使MySQL查詢的作品!在PHP解析XML不工作?

<?php 
    ... 
    $result = $mysqli->query($sql); 
    if ($result) {  //this query works, but no xml document produced as a result 
    $d = new DOMDocument(); 
    $books = $d->createElement('hey'); 
    $hey->setAttribute('check','The Adventures of Tom Sawyer'); 
    $d->appendChild($books); 
    $d->appendChild($hey); 
    $d->appendChild($books); 
    echo $d->saveXML(); 
} 
    ?> 
+0

'的var_dump($ d)'? – StasGrin

+0

似乎你必須使用這樣的東西:'$ d = $ d-> appendChild($ books);' – StasGrin

回答

2
$d->setAttribute('check','The Adventures of Tom Sawyer'); 

$ d 「是」 DOMDocument對象並沒有DOM文檔:: setAttribute()方法。
要麼使用DOMElement::setAttribute()DOMDocument::createAttribute()

if ($result) { 
    $d = new DOMDocument(); 
    $books = $d->createElement('hey'); 
    $books->setAttribute('check','The Adventures of Tom Sawyer'); 
    $d->appendChild($books); 
    echo $d->saveXML(); 
}