2014-02-20 103 views
0

我試圖用php從我的SQL Server數據庫中獲取數據,以便填充e XML文檔。但我有這個錯誤:Call to a member function saveXML() on a non-object in C:\xampp\htdocs\db\1mod.php on line 67 任何人都可以幫助我嗎?我能做什麼?在非對象上調用成員函數saveXML()

這是我的PHP文件

<?php 

    $file = fopen("C:/xampp/htdocs/result.xml","w"); 

    ini_set('display_errors',1); 
    error_reporting(E_ALL); 
    $server = "USER-PC\SQLEXPRESS"; 
    $options = array("UID"=>"", "PWD"=>"", "Database"=>"RESTORANTET"); 

    $conn = sqlsrv_connect($server, $options); 
    if(!$conn) 
    { 
    echo mysql_error(); 
    } 

    $Query = "SELECT MAX(ID_Rest) AS MAXID FROM RESTORANTET"; 
    $Result = sqlsrv_query($conn,$Query) 
    or die("Query to get blah failed with error111: ".mysql_error()); 
    $Row = sqlsrv_fetch_array($Result,SQLSRV_FETCH_ASSOC); 
    $nbPhase = $Row['MAXID']; 
    print "<TrafficPhase>"; 
    $i = 1; 
    $k = 1; 
    while ($i <= $nbPhase) 
    { 
     print "<Phase>\n"; 

     $Query1 = "SELECT * FROM RESTORANTET WHERE ID_Rest = $i"; 
     $Result1 = sqlsrv_query($conn,$Query1) or die("Query to get blah failed with error222: ".mysql_error()); 
     $Row1 = sqlsrv_fetch_array($Result1); 


     print "<Option_id>" .$k. "</Option_id>\n"; 
     print "<name>" .$Row1['Emri_Rest']. "</name>\n"; 
     print "<Option_Concept_ID>" .$Row1['Lloji_i_ushqimit']. "</Option_Concept_ID>\n"; 


     $i = $i + 1; 
     print "</Phase>\n"; 
    } 
    print "</TrafficPhase>\n"; 


    echo $file->saveXML(); 
    fwrite($file, $xml->w); 
    fclose($file); 


    ?> 

提前感謝!

回答

0

怎麼一回事,因爲saveXML是DOMDocument對象

http://pl1.php.net/domdocument.saveXML 

流的fopen犯規支持saveXML功能功能。如果你想使用它,你shuold創建DOM文檔的第一個對象,例如:

$dom = new DOMDocument('1.0', 'iso-8859-1'); 

echo $dom->saveXML(); /* <?xml version="1.0" encoding="iso-8859-1"?> */ 

如果你想去的XML元素作爲字符串進行操作,您可以使用

$sxe = new SimpleXMLElement('http://example.org/document.xml', NULL, TRUE); 
    echo $sxe->asXML(); 
相關問題