How to select all columns except one? - php

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>';
?>

Related

text declaration errror in php

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>';
?>

POST variable breaks the execution

Im trying to generate an xml with the code below, but I get this error message
without the POST, the xml is created normally.
require("conexao.php");
$variable = $_POST['cat1'];
$result_markers = "SELECT * FROM results where `cat` LIKE '
$variable'";
$resultado_markers = mysqli_query($link, $result_markers);
header("Content-type: text/xml");
echo '<markers>';
while ($row_markers = #mysqli_fetch_assoc($resultado_markers)){
echo '<marker ';
echo 'id="' . $row_markers['id'] . '" ';
echo 'name="' . parseToXML($row_markers['name']) . '" ';
echo '/>';
}
echo '</markers>';

Error on line 2 at column 10: xmlParseDocTypeDecl : no DOCTYPE name

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.

php and mysql to xml

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>';

Outputting XML via PHP and SQL

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' ?>";

Categories