XML数据库 - 二维数组

时间:2018-01-31 22:02:16

标签: xml

我试图从数据库中获取数据并将其分配给二维数组,并以XML格式显示。

我的XML格式是正确的,但是从数据库获取数据并分配给二维数组时出现错误。

我的代码如下。我该如何解决这个问题?

<?php

include_once 'dbh.inc.php';

$sql = "SELECT `employee_id`, `employee_fname`, `employee_lname`, `employee_department`, `employee_dateOfBirth`, `car_status` FROM `employee"; 

$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);

if ($resultCheck > 0) {
  while ( $row = mysqli_fetch_assoc($result)) {
    $employeesArray[$row['employee_id']] = array($row['employee_id'],$row['employee_fname'],$row['employee_lname'],$row['employee_department'],$row['employee_dateOfBirth'],$row['car_status']); 
  }
}

$doc = new DOMDocument('1.0');
$employeeList = $doc->createElement("employeeList");
$doc ->appendChild($employeeList);
$employeeInfo = $doc->createElement("employeeInfo");
$employeeList->appendChild($employeeInfo);

foreach ($employeesArray as $employee_id => $employeeINF) {
  $id_ = $doc->createElement("id",$employeeINF[0]);
  $employeeInfo->appendChild($id_);

  $name_ = $doc->createElement("name",$employeeINF[1]);
  $employeeInfo->appendChild($name_);

  $lname_= $doc->createElement("lastname",$employeeINF[2]);
  $employeeInfo->appendChild($lname_);

  $dateOfBirth_= $doc->createElement("dateOfBirth",$employeeINF[3]);
  $employeeInfo->appendChild($dateOfBirth_);

  $department_= $doc->createElement("department",$employeeINF[4]);
  $employeeInfo->appendChild($department_);

  $car_= $doc->createElement("car",$employeeINF[5]);
  $employeeInfo->appendChild($car_);
}

header('content-type: text/xml'); //Dosya tipi xml diye belirttik

echo $doc ->saveXML();

?>    

0 个答案:

没有答案