0

I am trying to create an XML file output in PHP for a remote phone book on an IP Phone, here is the code i have:

<?php
$conn=mysql_connect("localhost","user","********");
mysql_select_db("db_name",$conn);

header("Content-Type: text/xml");
header("Connection: close");
header("Expires: -1");
?>

<YealinkIPPhoneDirectory>
<?php
$output='<YealinkIPPhoneDirectory>\n';
$sql="SELECT * from contacts ";
$rs=mysql_query($sql,$conn);
while($result=mysql_fetch_array($rs)) {
    $output .= "<DirectoryEntry>\n";
    $output .= "<Name>Mobile:</Name>\n";
    $output .= "<Telephone>" . $result["mobile"] . "</Telephone>\n";
    $output .= "</DirectoryEntry>\n";
}
$output='</YealinkIPPhoneDirectory>\n';

echo '$output';
?>

but i get this error message:

This page contains the following errors:

error on line 3 at column 8: Extra content at the end of the document
Below is a rendering of the page up to the first error.

$output
4
  • 1
    Try echo $output; instead Commented Jan 31, 2014 at 18:06
  • This page contains the following errors: error on line 3 at column 27: Extra content at the end of the document Below is a rendering of the page up to the first error. Commented Jan 31, 2014 at 18:08
  • i get that error when i try echo $output; Commented Jan 31, 2014 at 18:09
  • You get what error? can't be the same... Commented Jan 31, 2014 at 18:12

2 Answers 2

2
<?php

$conn=mysql_connect("localhost","user","********");
mysql_select_db("db_name",$conn);

header("Content-Type: text/xml");
header("Connection: close");
header("Expires: -1");

$output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$output .= '<YealinkIPPhoneDirectory>\n';
$sql="SELECT * from contacts ";
$rs=mysql_query($sql,$conn);
while($result=mysql_fetch_array($rs)) {
    $output .= "<DirectoryEntry>\n";
    $output .= "<Name>Mobile:</Name>\n";
    $output .= "<Telephone>" . $result["mobile"] . "</Telephone>\n";
    $output .= "</DirectoryEntry>\n";
}
$output.='</YealinkIPPhoneDirectory>';

echo $output;

?>
Sign up to request clarification or add additional context in comments.

9 Comments

I'm now getting this : This page contains the following errors: error on line 1402 at column 27: Extra content at the end of the document Below is a rendering of the page up to the first error. but it shows all the results it should do. not in XML format though
it just echoes as a block of HTML text
Right, I think your XML is just coming out bad as that error seems to be on the application end I'm guessing. I also removed the reference to mysql_fetch_array... I always forget that grabs both array types by default.
Also, try adding $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; as the first line of your output.. Don't forget to change the = to .= for the second line and see what you get.
tried all that - now getting error on line 2 at column 6: XML declaration allowed only at the start of the document
|
0

You need to remove this line:

echo '$output';

and replace it with this:

echo $output;

Here is a sample:

https://eval.in/96740

Note how singlequoted values are literally echoed, so you literally see $output as your output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.