Little help required
I keep getting an error :
"XML Parsing Error: XML or text declaration not at start of entity
Location: http://localhost/test/
Line Number 2, Column 1:"
in the inspector on Firefox it comes up with ^
I know that the problem will be with the ^ but I can't seem to find any whitespaces in the below code. Also in the source code the
Just to clarify I am trying to following the instructions here: Using PHP's DOM XML functions to output XML
Any help is appreiciated.
Thanks
<?php
// require("phpsqlajax_dbinfo.php");
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
// Opens a connection to a MySQL server
$connection=mysqli_connect ('localhost', $username, $password);
if (!$connection) {
die('Not connected : ' . mysqli_error());
}
// Set the active MySQL database
$db_selected = mysqli_select_db($connection, $database);
if (!$db_selected) {
die ('Can\'t use db : ' . mysqli_error($connection));
}
// Select all the rows in the markers table
$query = "SELECT * FROM addresses WHERE 1";
$result = mysqli_query($connection, $query);
if (!$result) {
die('Invalid query: ' . mysqli_error($connection));
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<?xml version="1.0"?>';
echo '<markers>';
$ind=0;
// Iterate through the rows, printing XML nodes for each
while ($row = #mysqli_fetch_assoc($result)){
// Add to XML document node
echo '<marker ';
echo 'id="' . $row['id'] . '" ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo '/>';
$ind = $ind + 1;
}
// End XML file
echo '</markers>';
?>
Related
I'm trying to create an RSS feed for my site, I've got 99% of it working, however I'm currently having a problem. When I go to input the description field it presents me with an encoding error. The description field in the database is text so I presumed it would work.
<?php
// Login to Databse
$link = mysqli_connect("DB_HOST", "DB_USER", "DB_PASS", "DB_NAME");
// Attempt select query execution
$sql = "SELECT * FROM posts";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
// RSS XML Header
header( "Content-type: text/xml");
echo "<?xml version='1.0' encoding='UTF-8'?>";
echo "<rss version='2.0'>";
echo "<channel>";
echo "<link>https://" . $_SERVER['SERVER_NAME'] . "</link>";
echo "<language>en</language>";
// RSS Add Content
while($row = mysqli_fetch_array($result)){
echo "<item>";
echo "<title>" . $row['title'] . "</title>";
echo "<link>https://" . $_SERVER['SERVER_NAME'] . "/" . $row['self'] . "-" . $row['id'] . "</link>";
echo "<description>" . $row['description'] . "</description>";
echo "<enclosure url='https://" . $_SERVER['SERVER_NAME'] . "/public/upload/cover/" . $row['image'] . "' length='174093' type='image/webp'/>";
echo "</item>";
}
// End RSS Feed
echo "</channel>";
echo "</rss>";
// End Feed
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
My data types are varchar's and int's, even if I change the text to varchar I get the same error:
error on line 1 at column 596: Encoding error
I'm hoping I'm just been stupid but any help would be greatly appreciated.
How can I select all columns in my database except the one the user is on?
I want to have all data posted in my xml page, but not the line that shows the current users info
<?php
include_once 'test-dbconnect.php';
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
$sql = "select * from tbl_users WHERE username != '".$row['username']."'";
$result = mysqli_query($DBcon, $sql) or die("Error in Selecting " . mysqli_error($DBcon));
//create an array
$user_data = array();
while($row =mysqli_fetch_assoc($result))
{
$user_data[] = $row;
echo '<marker ';
echo 'name="' . parseToXML($row['username']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'latitude="' . $row['latitude'] . '" ';
echo 'longitude="' . $row['longitude'] . '" ';
echo '/>';
}
//close the db connection
mysqli_close($DBcon);
// End XML file
echo '</markers>';
?>
Just got this error, have no idea why, can anyone help?
error on line 2 at column 10: xmlParseDocTypeDecl : no DOCTYPE name !
I am trying to take information from my database and output it into XML when I run it in the browser.
Here is my code
<?php
include 'header.php';
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
// Select all the rows in the Blog table
$query = "SELECT * FROM Blog WHERE 1";
$result = mysqli_query($con, $query);
if (!$result)
{
die('Invalid query: ' . mysqli_error($con));
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<blog>';
// Iterate through the rows, printing XML nodes for each
while ($row = #mysqli_fetch_assoc($result))
{
// ADD TO XML DOCUMENT NODE
echo '<blogs ';
echo 'ID="' . parseToXML($row['ID']) . '" ';
echo 'subject="' . parseToXML($row['subject']) . '" ';
echo 'content="' . parseToXML($row['content']) . '" ';
echo 'latitude="' . $row['latitude'] . '" ';
echo 'longitude="' . $row['longitude'] . '" ';
echo 'imageName="' . parseToXML($row['imageName']) . '" ';
echo 'datetime="' . parseToXML($row['datetime']) . '" ';
echo '/>';
}
// End XML file
echo '</blog>';
?>
Turns out no DOCTYPE name literally means, I had not named my DOCTYPE.
I had
<!DOCTYPE>
Where I should have had
<!DOCTYPE html>
Normally this would not cause a problem but since I was trying to output XML data everything has to be perfect in the Document Tree.
i have 2 tables, first one which stores restaurant info and second stores dishes served in each. They are linked using res_id.
1) info_main [id, res_id, res_name,res_pc]
2) dishes [id,dishName,price,res_id(Foreign key)]
My SQL query is
$query = "SELECT * FROM info_main LEFT JOIN dishes ON info_main.res_id = dishes.res_id";
I am inserting the results from the query into an xml file which works fine. Below is the code:
$query = "SELECT * FROM info_main LEFT JOIN dishes ON info_main.res_id = dishes.res_id";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = #mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<marker> ';
echo '<detail1>';
echo '<resdetails ';
echo 'name="' . parseToXML($row['res_name']) . '" ';
echo 'id="' . parseToXML($row['res_ID']) . '" ';
echo 'pc="' . parseToXML($row['res_pc'] ). '" ';
echo '/>';
echo '<dishdetails ';
echo 'name="' . parseToXML($row['dishName']) . '" ';
echo 'price="' . parseToXML($row['price']) . '" ';
echo '/>';
echo '</detail1>';
echo '</marker>';
}
This work fine however if a restaurant has 3 dishes in the database, then xml create 3 nodes: Something like this:
I want the xml structure like
<detail1>
<resdetails name="spoted dog" id="xyz" pc="xyz"/>
<dishdetails name="bean burger" price="1" />
<dishdetails name="cheese and tomato panini" price="3" />
<dishdetails name="veg salad" price="2" />
</details1>
I cant figure out a way how to achieve the above stated xml structure. Your help is greatly appreciated. Thanks
$query = "SELECT * FROM info_main LEFT JOIN dishes ON info_main.res_id = dishes.res_id";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
$resname = "";
while ($row = #mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
if ($resname!=$row['res_name']) {
//restname isn't populated or doesn't match current so output new headers
echo '<marker> ';
echo '<detail1>';
echo '<resdetails ';
echo 'name="' . parseToXML($row['res_name']) . '" ';
echo 'id="' . parseToXML($row['res_ID']) . '" ';
echo 'pc="' . parseToXML($row['res_pc'] ). '" ';
echo '/>';
}
//this bit needs to always happen
echo '<dishdetails ';
echo 'name="' . parseToXML($row['dishName']) . '" ';
echo 'price="' . parseToXML($row['price']) . '" ';
echo '/>';
if ($resname!=$row['res_name']) {
//restname isn't populated or doesn't match current so output new headers
echo '</detail1>';
echo '</marker>';
}
$resname = $row['res_name']; //set resname to this res_name as this is our check to see if we've already put out required headers for this item that way every change it'll put this back in
}
Something like this (note may need some tidying up)
For that structure just do this:
echo '<marker> ';
echo '<detail1>';
while ($row = #mysql_fetch_assoc($result)){
echo '<dishdetails ';
echo 'name="' . parseToXML($row['dishName']) . '" ';
echo 'price="' . parseToXML($row['price']) . '" ';
echo '/>';
}
echo '</detail1>';
echo '</marker>';
All that is really different, is that I moved a portion of your code out of the while loop.
Atlast i got that working, i used two query's, one getting distinct restarant and other getting dishname. i used loop in the main loop. below is the code.
$query = "SELECT DISTINCT * FROM info_main";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = #mysql_fetch_assoc($result)){
$id=$row['res_id'];
// ADD TO XML DOCUMENT NODE
echo '<marker> ';
echo '<detail1>';
echo '<resdetails ';
echo 'name="' . parseToXML($row['res_name']) . '" ';
echo 'id="' . parseToXML($id) . '" ';
echo 'pc="' . parseToXML($row['res_pc'] ). '" ';
echo '/>';
$dishes = "SELECT * FROM dishes WHERE res_id = '" . $id . "'";
$dish = mysql_query($dishes);
while ($row2 = #mysql_fetch_assoc($dish)){
$id2 = $row2['res_id'];
echo '<dishdetails ';
echo 'name="' . parseToXML($row2['dishName']) . '" ';
echo 'price="' . parseToXML($row2['price']) . '" ';
echo '/>';
}
echo '</detail1>';
echo '</marker>';
}
echo '</markers>';
Can anyone help me in by telling me how can I output my XML result via PHP? I have an sql database and written the function in PHP to parse the XML and I have debugged the page in Firefox (using the NET tab) which is bringing up a the correct response corresponding to my SQL statement however, I can't actually see the data, its just a blank page.
Here is the php file to write the XML:
<?php
include("classes/database_connection.php");
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
// Opens a connection to a MySQL server
$connection=mysql_connect ('www.numyspace.co.uk', '*********', '************');
if (!$connection) {
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('********', $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the ticket table
$query = "SELECT * FROM ticket";
$result = mysql_query($query);
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<ticket>';
while ($row = #mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<tickets ';
echo 'ticketID="' . parseToXML($row['ticketID']) . '" ';
echo 'locationID="' . parseToXML($row['locationID']) . '" ';
echo 'venue="' . parseToXML($row['venue']) . '" ';
echo 'tPrice="' . parseToXML($row['tPrice']) . '" ';
echo 'date="' . parseToXML($row['date']) . '" ';
echo 'availability="' . parseToXML($row['availability']) . '" ';
echo 'time="' . parseToXML($row['time']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo '/>';
}
// End XML file
echo '</ticket>';
?>
Thank you and any help is much appreciated.
EDIT: The page's markup:
<ticket><tickets ticketID="1" locationID="1" venue="The Cluny" tPrice="15" date="2012-04-17" availability="200" time="20:00:00" lat="54.978252" lng="-1.617780" /><tickets ticketID="2" locationID="1"..../></ticket>
Ensure you have the XML declaration at the top of your XML.
echo "<?xml version='1.0' ?>";