2

誰かが私を助けてくれたらとても嬉しいです:)

Oracle DBからクエリを読み取り、結果をXMLとして表示するphpWebサービスを作成しようとしています。私は解決策を探すのに何日も費やしましたが、残念ながらそれはうまくいきませんでした

以下は私が試したいくつかのクエリです:

    <?php
  $db ="(DESCRIPTION=
         (ADDRESS_LIST=
           (ADDRESS=(PROTOCOL=TCP)(HOST=xxxx)(PORT=1530))
          )
         (CONNECT_DATA=(SID=DEV))
       )";
$c = oci_connect("uname", "pass", $db);

$q = "select dbms_xmlgen.getxml(
       'select user_name
        from fnd_user
        where user_name = 007144') xml
      from dual";

$s = oci_parse($c, $q);
oci_execute($s);
$r = oci_fetch_array($s, OCI_ASSOC);
$mylob = $r['XML']->load();   // Treat column data as a LOB descriptor

echo "<pre>";
echo htmlentities($mylob);
echo "</pre>";
?>

2番目のコード:

<?php
//File: DOM.php
$db="(DESCRIPTION=
     (ADDRESS_LIST=
       (ADDRESS=(PROTOCOL=TCP)
         (HOST=xxx)(PORT=1530)
       ) )
       (CONNECT_DATA=(SID=DEV))
 )";
 $conn = OCILogon("uname","pass",$db);
 if ($conn) {
   //echo "Successfully connected to Oracle.";  
 } else {
   $err = oci_error();
   echo "Oracle Connect Error " . $err['text'];
 }
$query = "SELECT user_name from fnd_user where user_name = '007144'";
$stmt = oci_parse($conn,$query);

if (!oci_execute($stmt, OCI_DEFAULT)) {
$err = oci_error($stmt);
trigger_error('Query failed: ' . $err['message'], E_USER_ERROR);
}
$dom = new DOMDocument('1.0', 'UTF-8');
$root = $dom->createElement('EMPLOYEES', 'Root');
$dom->appendChild($root);//$root = $dom->appendChild($root);
while ($row = oci_fetch_array($stmt, OCI_RETURN_NULLS)) 
{
$emp = $dom->createElement('EMPLOYEE', 'emp');
$emp = $root->appendChild($emp);
$emp->setAttribute('id', $row['user_name']);

/*
$emp = $dom->createElement('EMPLOYEE', '');
                $emp = $root->appendChild($emp);
                $emp->setAttribute('id', $row['user_name']);
                $ename = $dom->createElement('ENAME', $row['user_name']);
                $ename = $emp->appendChild($ename);
                $salary = $dom->createElement('SALARY', $row['user_name']);
                $salary = $emp->appendChild($salary);
*/
}
        echo $dom->saveXML();
        //$dom->save("employees.xml");
oci_close($conn); 
?>

上記のコードは、下の図のようにデータなしのXMLを提供します。 http://i48.tinypic.com/2lnv5eo.jpg

3番目のコード

<?php
  //File: XMLFromSQL.php
  $user = 'uname';
  $pswd = 'pass';
  $db ='(DESCRIPTION=
         (ADDRESS_LIST=
           (ADDRESS=(PROTOCOL=TCP)(HOST=xxx)(PORT=1530))
          )
         (CONNECT_DATA=(SID=DEV))
       )';

 $sql = "SELECT user_name as RESULT FROM  fnd_user 
          WHERE user_name=:deptno";
  $deptno = 007144;
 //Connect to the database and obtain info on a given department in XML format
  $conn = oci_connect($user, $pswd, $db);

 $query = oci_parse($conn, $sql);
  oci_bind_by_name($query, ":deptno", $deptno, 2);
  oci_execute($query);
  oci_fetch($query);
  $strXMLData = oci_result($query, 'RESULT');

  //Create a new DOM document and load XML into its internal XML tree
  $doc = new DOMDocument("1.0", "UTF-8");
  $doc->loadXML($strXMLData);

  //For simplicity, just print out the XML document
  print $doc->saveXML();
?>

別の方法があれば私を助けてください、さもないと私は夢中になります:(

4

2 に答える 2

0

私はあなたのファイルをテストしました:

$row['user_name']

C:\Apps\PHP>php c:\temp\test.php
PHP Notice:  Undefined index: user_name in C:\temp\test.php on line 25

Notice: Undefined index: user_name in C:\temp\test.php on line 25
<?xml version="1.0" encoding="UTF-8"?>
<EMPLOYEES>Root<EMPLOYEE id="">emp</EMPLOYEE></EMPLOYEES>

$row['USER_NAME']

C:\Apps\PHP>php c:\temp\test.php
<?xml version="1.0" encoding="UTF-8"?>
<EMPLOYEES>Root<EMPLOYEE id="007144">emp</EMPLOYEE></EMPLOYEES>

それはあなたの問題を解決しますか?

編集:ソリューション1の場合(PLSQL側)

$s = oci_parse($c, $q);
oci_execute($s, OCI_DEFAULT);
$r = oci_fetch_array($s, OCI_RETURN_NULLS+OCI_RETURN_LOBS);
echo "<pre>";
echo htmlentities($r["XML"]);
echo "</pre>";

または要素の名前をより細かく制御できるようにする

$q = "select xmlelement(
         \"EMPLOYEES\",
         xmlagg(
           xmlelement(
             \"EMPLOYEE\", 
             xmlforest(user_name as \"EmpName\",
                       id as \"EmpID\",
                       email as \"email\"
                      )
           )
         )
       ).getclobval() xml
  from fnd_user";

$s = oci_parse($c, $q);
oci_execute($s, OCI_DEFAULT);
$r = oci_fetch_array($s, OCI_RETURN_NULLS+OCI_RETURN_LOBS);
echo "<pre>";
echo htmlentities($r["XML"]);
echo "</pre>";

xmlforestバージョンでの出力例は次のとおりです。

C:\Apps\PHP>php c:\temp\test3.php
<pre>&lt;EMPLOYEES&gt;&lt;EMPLOYEE&gt;&lt;EmpName&gt;John Doe&lt;/EmpName&gt;&lt;EmpID&gt;1&lt;/EmpID&gt;&lt;email&gt;d@d.com&lt;/email&gt;&lt;/EMPLOYEE&gt;&lt;EMPLOYEE&gt;&lt;
EmpName&gt;Alan Smith&lt;/EmpName&gt;&lt;EmpID&gt;2&lt;/EmpID&gt;&lt;email&gt;e@e.com&lt;/email&gt;&lt;/EMPLOYEE&gt;&lt;/EMPLOYEES&gt;</pre>
C:\Apps\PHP>

すなわち:

<EMPLOYEES>
    <EMPLOYEE>
        <EmpName>John Doe</EmpName>
        <EmpID>1</EmpID>
        <email>d@d.com</email>
    </EMPLOYEE>
    <EMPLOYEE>
        <EmpName>Alan Smith</EmpName>
        <EmpID>2</EmpID>
        <email>e@e.com</email>
    </EMPLOYEE>
</EMPLOYEES>
于 2012-11-22T17:17:27.987 に答える
0

最後に、このXMLを取得します

<?php
//File: DOM.php
$db="(DESCRIPTION=
     (ADDRESS_LIST=
       (ADDRESS=(PROTOCOL=TCP)
         (HOST=xxx)(PORT=1530)
       )
     )
       (CONNECT_DATA=(SID=DEV))
 )";
 $conn = OCILogon("uname","pass",$db);
 if ($conn) {
   //echo "Successfully connected to Oracle.";  
 } else {
   $err = oci_error();
   echo "Oracle Connect Error " . $err['text'];
 }
//$dept_id = 007144;
$query = "SELECT user_name from fnd_user where user_name = '007144'";


$stmt = oci_parse($conn,$query);
//oci_bind_by_name($stmt, ':deptid', $dept_id);
if (!oci_execute($stmt, OCI_DEFAULT)) {
$err = oci_error($stmt);
trigger_error('Query failed: ' . $err['message'], E_USER_ERROR);
}
$dom = new DOMDocument('1.0', 'UTF-8');
$root = $dom->createElement('EMPLOYEES', '');
$dom->appendChild($root);//$root = $dom->appendChild($root);
while ($row = oci_fetch_array($stmt, OCI_RETURN_NULLS)) 
{

//var_dump($row);
//$emp = $dom->createElement('EMPLOYEE', 'emp');
//$emp = $root->appendChild($emp);
//$emp->setAttribute('id', $row['EMPLOYEE_NUMBER']);

$emp = $dom->createElement('EMPLOYEE', '');
                $emp = $root->appendChild($emp);
                //$emp->setAttribute('id', $row['FULL_NAME']);
                $EmpName = $dom->createElement('EmpName', $row['FULL_NAME']);
                $EmpName = $emp->appendChild($EmpName);
                $empid = $dom->createElement('EmpID', $row['EMPLOYEE_NUMBER']);
                $empid = $emp->appendChild($empid);
                $email = $dom->createElement('email', $row['EMAIL_ADDRESS']);
                $email = $emp->appendChild($email);
}
        echo $dom->saveXML();
        $dom->save("employees.xml");
oci_close($conn); 
?>

そしてここでの結果:http: //filedb.experts-exchange.com/incoming/2012/11_w47/617768/XMLinPHP4.jpg

今、私はこのXMLの解析を開始し、最終的にうまくいくことを願っています。そうしないと、戻ってきてまた邪魔をします、ごめんなさい:(

明日お知らせします、待ってください:)

于 2012-11-23T16:59:28.907 に答える