可捕获的致命错误:传递给DOMNode :: appendChild()的参数1必须是DOMNode的一个实例,null给定,

时间:2017-12-18 11:30:59

标签: php dom

我无法使用PHP将我的mysql表导出为XML格式,运行以下脚本时收到以下错误。我已经删除了连接位,我已经经历过错误,但无法解决此问题。 可捕获的致命错误:传递给DOMNode :: appendChild()的参数1必须是DOMNode的一个实例,null给定,

脚本:

<?php
$query = "SELECT id, name, plate, datetime , time , image_name FROM EarlsdonMSIN_anpr_vega";
$camArray = array();
if ($result = $mysqli->query($query)) {
    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
       array_push($camArray, $row);
    }
      if(count($camArray)){

        createXMLfile($camArray);

     }

    /* free result set */
    $result->free();
}

/* close connection */
$mysqli->close();

function createXMLfile($camArray){

   $filePath = '/cam.xml';

   $dom     = new DOMDocument('1.0', 'utf-8'); 

   $root      = $dom->createElement('cam'); 

   for($i=0; $i<count($camArray); $i++){

     $camId        =  $camArray[$i]['id'];  

     $camName      =  $camArray[$i]['name']; 

     $camplate    =  $camArray[$i]['plate']; 

     $camdatetime     =  $camArray[$i]['datetime']; 

     $camtime      =  $camArray[$i]['time']; 

     $camimagename  =  $camArray[$i]['image_name']; 

     $cam = $dom->createElement('cam');

     $cam->setAttribute('id', $camId);

     $name     = $dom->createElement('name', camName); 

     $cam->appendChild($name); 

     $plate   = $dom->createElement('plate', $camplate); 

     $cam->appendChild($plate); 

     $datetime    = $dom->createElement('datetime', $camdatetime); 

     $cam->appendChild($datetime); 

     $time     = $dom->createElement('time', $camtime); 

     $cam->appendChild($time); 
     $imagename = $dom->createElement('image_name', $camimagename);                           $cam->appendChild($image_name);
      $root->appendChild($cam);
   }
   $dom->appendChild($root); 
   $dom->save($filePath); 
} 

0 个答案:

没有答案