PHP MYSQL XML ERROR - php

I have a PHP file which reads the credentials from
require("phpRequireInfo.php");
And, my problem is that it keeps giving me error whenever i were to compile it. My php file looks like this:
<?php
header("Content-type: text/xml");
require("phpRequireInfo.php");
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// Opens a connection to a MySQL server
$connection=mysql_connect ($database, $username, $password);
if (!$connection) { die('Not connected : ' . mysql_error());}
// Set the active MySQL database
$dbname= 'csuser';
// Set the active MySQL database
$db_selected = mysql_select_db($dbname, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM Bars ";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
// Iterate through the rows, adding XML nodes for each
while ($row = #mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name",$row['Name'] );
}
echo $dom->saveXML();
?>
And the error i am getting is: "error on line 4 at column 6: XML declaration allowed only at the start of the document"
The XML File looks like this when running the PHP FILE:
http://imgur.com/SWCZ8sE
Your help will greatly be appreciated

Why does it has 3 empty lines, at the begining?
try ob_start() before require, and ob_end_clean() before echo, to avoid them
ob_start();
require("phpRequireInfo.php");
.
.
.
ob_end_clean();
echo $dom->saveXML();

Related

Not able to create xml file on remote server using php

I am developing a php app where I need to fetch some data from MySQL database and create an xml file. I have succeeded in doing that in my local server(XAMPP on Windows) but, when I tried to load it from remote server(Linux) I am not getting any output.
PHP Code:
<?php
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
$connection=mysql_connect ($host, $username, $password);
if (!$connection) { die('Not connected : ' . mysql_error());}
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
$query = "SELECT * FROM tracker WHERE 1 LIMIT 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
while ($row = #mysql_fetch_assoc($result)){
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['lng']);
}
echo $dom->saveXML();
?>
Output I got on local machine:
`
<markers>
<marker lat="17" lng="77"/>
</markers>
On remote server I am getting a 500 Internal Server Error.
The cause can be many-fold - access rights, xml libraries you are missing, php version...
can you check the error_log file
/var/log/httpd/error_log on RHEL
or
/var/log/apache2/error_log on DEB
and paste the output from the error?

XML Parsing Error: junk after document element__wampserver

I tried to create XML from database, but I'm getting an error when I run my PHP code in WampServer:
XML Parsing Error: junk after document element
Line Number 2, Column 1:<font size='1'><table class='xdebug-error'
dir='ltr' border='1' cellspacing='0' cellpadding='1'>
my code from code.google:
{<?php
require("phpsqlajax_dbinfo.php");
// Start XML file, create parent node
$doc = new DOMDocument('1.0', 'iso-8859-1');
$node = $doc->createElement("place");
$parnode = $doc->appendChild($node);
// Opens a connection to a MySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the place table
$query = "SELECT * FROM place WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node = $doc->createElement("place");
$newnode = $parnode->appendChild($node);
$newnode->set_attribute("place_id", $row['place_id']);
$newnode->set_attribute("p_name", $row['p_name']);
$newnode->set_attribute("lat", $row['lat']);
$newnode->set_attribute("lng", $row['lng']);
$newnode->set_attribute("addr", $row['addr']);
$newnode->set_attribute("tel", $row['tel']);
$newnode->set_attribute("category", $row['category']);
$newnode->set_attribute("description", $row['description']);
}
$xmlfile = $doc->dump_mem();
echo $xmlfile;
?>
This is is table:
place_id(int),
p_name(varchar),
lat(float),
lng(float),
addr(varchar),
tel(int),
category(varchar),
description(varchar)
Your code is generating an error. As message mentioned xdebug-error this indicates that instead of valid code you have an error placed somewhere.
Try to save your XML to some file or comment your header("Content-type: text/xml"); to see whats going on.
I had the same problem with google maps php/mysql tutorial. Found that it works if I put single quotes on localhost ex $connection=mysql_connect ('localhost', $username, $password);

XML Extra content at the end of the document (Using PHP SimpleXML)

I want to create a app for search properties with zip code of corresponding areas and locate them on google map.I am testing Google's tutorial to use Maps with PHP/MySql but stuck in XML.
Its giving me this error:-
This page contains the following errors:
error on line 2 at column 1: Extra content at the end of the document
Below is a rendering of the page up to the first error.
This is the Code:
<?php
$username="root";
$password="";
$database="test";
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// Opens a connection to a MySQL server
$connection=mysql_connect ('localhost', $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM markers";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = #mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name", $row['name']);
$newnode->setAttribute("address", $row['address']);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['lng']);
$newnode->setAttribute("distance", $row['distance']);
}
$smd=$dom->saveXML();
echo $smd;
?>
mostly this error results when no XML Root element is seen or not unique!
in your case: I guess your XML creation failed with an die() and your external script parsing this PHP output will not recognize it as a valid XML?!
Also the comment above by StasGrin might be a good point:
Remove the blanks in front of in the end!
Both will render blanks, but this should also result in an PHP Error / Warning:
Warning: Cannot modify header information - headers already sent by (output started at …) in …

Save to a XML document using php problem

Hey all. I'm trying to save A XML file using php. here is my code, the connection to the DB is okay and all, i can use $dom->saveXML() but the save function isn't saving nothing.
please help.
<?
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM Listing_Arnona WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
// Iterate through the rows, adding XML nodes for each
while ($row = #mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("Price",$row['Price']);
$newnode->setAttribute("PriceS", $row['Price']);
$newnode->setAttribute("address", $row['street_name']);
$newnode->setAttribute("NumRooms", $row['Room_Num']);
$newnode->setAttribute("PROMO", $row['PROMO']);
}
$dom->formatOutput = true;
$test1 = $dom->saveXML(); // put string in test1
$dom->save('test1.xml'); // save as file
?>
The problem as everyone pointed out was the writing permissions. i opend a new folder name 'xml' with chmod 777 and changed the code to:
$dom->save('xml/test1.xml');
Thank you all.

Using PHP/MySQL with Google Maps

Update 2: So I never have used the debug function in firebug, but I just looked at the script section and I get this, now I will try and figure this out.
Failed to load source for:
http://localhost/googleMap/phpsqlajax_genxml.php
Hiya,
I followed this tutorial below
http://code.google.com/apis/maps/articles/phpsqlajax_v3.html#outputxml
I ran into trouble, near then end, I am hoping someone else here has
got this working and can help me discover my problem. Simply there are 4
steps to this tutorial
Creating the Table
Populating the Table
Outputting XML with PHP
Creating the Map
I successfully have completed all the steps, however the outputted xml
isn't read by the google map I created. The files are all on the same
directory, and I didn't change any of the file names from the
tutorial. The tutorial has a step to test if the php file called
phpsqlajax_genxml.php is outputting the xml and I successfully tested
it and it was.
The problem is that the map isn't rendering the items I have in the
database, that should be converted to xml for the map to read.
Any help, or pointing me in the right direction would be much
appreciated.
UPDATE 1: I realize I don't have any code to show here, there are just 3 files so I am not sure which will be of the best use. I Have a question that might help my issue though.
In the xml output part of the tutorial I am asked too
Call this PHP script from the browser
to make sure it's producing valid XML.
If you suspect there's a problem with
connecting to your database, you may
find it easier to debug if you remove
the line in the file that sets the
header to the text/xml content type,
as that usually causes your browser to
try to parse XML and may make it
difficult to see your debugging
messages.
This is the phpsqlajax_dbinfo.php file
<?php
$username="root";
$password="root";
$database="root-googleMap";
?>
This is the code for the generate xml, Is a xml document actually made after and can I open in, or is it temporary conversion. If so How do i do the step above to test, I dont really understand.
<?php
require("phpsqlajax_dbinfo.php");
// Start XML file, create parent node
$doc = domxml_new_doc("1.0");
$node = $doc->create_element("markers");
$parnode = $doc->append_child($node);
// Opens a connection to a mySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active mySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = #mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node = $doc->create_element("marker");
$newnode = $parnode->append_child($node);
$newnode->set_attribute("name", $row['name']);
$newnode->set_attribute("address", $row['address']);
$newnode->set_attribute("lat", $row['lat']);
$newnode->set_attribute("lng", $row['lng']);
$newnode->set_attribute("type", $row['type']);
}
$xmlfile = $doc->dump_mem();
echo $xmlfile;
?>
I'm assuming (and hoping) you're not using PHP 4 anymore and that this is the source of your problem, the functions relating to the DOM XML manipulation have been replaced with the DOMDocument class, which has different method names.
As such, I have refactored your code to be compatible with PHP 5:
<?php
require("phpsqlajax_dbinfo.php");
// Start XML file, create parent node
$doc = new DOMDocument("1.0");
$node = $doc->createElement("markers");
$parnode = $doc->appendChild($node);
// Opens a connection to a mySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active mySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = #mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node = $doc->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->set_attribute("name", $row['name']);
$newnode->set_attribute("address", $row['address']);
$newnode->set_attribute("lat", $row['lat']);
$newnode->set_attribute("lng", $row['lng']);
$newnode->set_attribute("type", $row['type']);
}
echo $doc->saveXML();
?>

Categories