Samurai Legend Posted June 29, 2019 Share Posted June 29, 2019 (edited) Hello, as most of you know I am learning PHP. In order for me to learn, I like to practise and make things. Hopefully soon when I have enough experience Ill be moving onto Laravel and learn more about the framework. Error - This page contains the following errors: error on line 1 at column 1: Document is empty Below is a rendering of the page up to the first error. File - <?php include 'configuration/connect.php'; try { $database = new Connection(); $db = $database->openConnection(); // Get parameters from URL //$center_lat = $_GET["lat"]; //$center_lng = $_GET["lng"]; //$radius = $_GET["radius"]; $center_lat = (array_key_exists('lat', $_GET) && is_string($_GET['lat'])) ? $_GET['lat'] : ''; $center_lng = (array_key_exists('lng', $_GET) && is_string($_GET['lng'])) ? $_GET['lng'] : ''; $radius = (array_key_exists('radius', $_GET) && is_string($_GET['radius'])) ? $_GET['radius'] : ''; // Start XML file, create parent node $dom = new DOMDocument("1.0"); $node = $dom->createElement("markers"); $parnode = $dom->appendChild($node); $sql = "SELECT bID, bNAME, bADDRESS, bLAT, bLNG, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM bookers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20"; header("Content-type: text/xml"); // Iterate through the rows, adding XML nodes for each foreach ($db->query($sql) as $row) { $node = $dom->createElement("marker"); $newnode = $parnode->appendChild($node); $newnode->setAttribute("id", $row['bID']); $newnode->setAttribute("name", $row['bNAME']); $newnode->setAttribute("address", $row['bADDRESS']); $newnode->setAttribute("lat", $row['bLAT']); $newnode->setAttribute("lng", $row['bLNG']); $newnode->setAttribute("distance", $row['distance']); } echo $dom->saveXML(); $database->closeConnection(); } catch (PDOException $e) { echo "There is some problem in connection: " . $e->getMessage(); } ?> I believe it is something got to do with the XML part. However, I can not seem to see what is wrong? ? no one can't help me? Edited June 29, 2019 by Samurai Legend Quote Link to comment Share on other sites More sharing options...
Shades Posted June 30, 2019 Share Posted June 30, 2019 (edited) Try this, came across something similar to this. However, I don't know what I have outputted below will work. - <?php error_reporting(E_ALL & ~E_NOTICE); require("configuration/connect.php"); // Start XML file, create parent node $doc = new DOMDocument('1.0', 'UTF-8'); $node = $doc->createElement("markers"); try { $database = new Connection(); $db = $database->openConnection(); ///$sql = 'SELECT * FROM markers'; // Set the appropriate content-type header and output the XML //header('Content-type: text/xml'); $stmt = $db->prepare('SELECT * FROM markers'); $stmt->setFetchMode(PDO::FETCH_ASSOC); $stmt->execute(); $result = $stmt->fetchAll(); // Iterate through the rows, adding XML nodes for each foreach ($result as $row) { $entry = $doc->createElement('markers'); $entry->setAttribute('name', $row['bNAME']); $node->appendChild($entry); } $doc->appendChild($node); echo $doc->saveXML(); $database->closeConnection(); } catch (PDOException $e) { echo "There is some problem in connection: " . $e->getMessage(); } ?> Edited June 30, 2019 by Shades 2 Quote Link to comment Share on other sites More sharing options...
Uridium Posted June 30, 2019 Share Posted June 30, 2019 im not sure but try not spacing the <?php example line 1 <?php rather than line 1 <?php Quote Link to comment Share on other sites More sharing options...
Samurai Legend Posted June 30, 2019 Author Share Posted June 30, 2019 (edited) @Shades Man, thanks for the reply! I've been waiting as I tried so much to get it working. It always throws errors at me. I used what you provided me. When I go on the file, there is no errors it just shows blank white web page. However, there's no XML file created? If someone knows the answer and can help please let me know! If any other information is needed please let me know. What I am working on is very important to me. Thanks once again for helping me out. 3 minutes ago, Uridium said: im not sure but try not spacing the <?php example line 1 <?php rather than line 1 <?php I think the spacing was created by the forums...On Sublime Text it has no spacing. Thanks @Uridum for replying and taking your time to read. I appreciate it. @Uridium- Thank you so much for helping me out privately, without you I wouldn't have been able to fix the issue. + 1 @Shades - Thank you too, for providing a better copy of what I wrote although it didn't work, it still helped me out big time as your fixes helped me and Illusion remove the main issue. Edited June 30, 2019 by Samurai Legend 1 Quote Link to comment Share on other sites More sharing options...
Samurai Legend Posted July 1, 2019 Author Share Posted July 1, 2019 $query = sprintf("SELECT id, name, address, lat, lng, type, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20", $center_lat, $center_lng, $center_lat, $radius); $stmt = $db->prepare($query); $stmt->setFetchMode(PDO::FETCH_ASSOC); $stmt->execute(); $result = $stmt->fetchAll(); Now when using this, it doesn't create which makes an empty XML file. Can anyone see the problem? Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted July 2, 2019 Share Posted July 2, 2019 That particular snippet is the database result. That ain't the XML part Quote Link to comment Share on other sites More sharing options...
Samurai Legend Posted July 6, 2019 Author Share Posted July 6, 2019 @Magictallguy <?php error_reporting(E_ALL & ~E_NOTICE); require("configuration/connect.php"); // Start XML file, create parent node $doc = new DOMDocument('1.0', 'UTF-8'); $node = $doc->createElement("markers"); try { $database = new Connection(); $db = $database->openConnection(); // Get parameters from URL $center_lat = $_GET["lat"]; $center_lng = $_GET["lng"]; $radius = $_GET["radius"]; //$query = 'SELECT * FROM markers'; // Set the appropriate content-type header and output the XML header('Content-type: text/xml');*$query = sprintf("SELECT id, name, address, lat, lng, type, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20", $center_lat, $center_lng, $center_lat, $radius); $distance = 0; $stmt = $db->prepare($query); $stmt->setFetchMode(PDO::FETCH_ASSOC); $stmt->execute(); $result = $stmt->fetchAll(); // Iterate through the rows, adding XML nodes for each foreach ($result as $row) { $entry = $doc->createElement('marker'); $entry->setAttribute('id', $row['id']); $entry->setAttribute('name', $row['name']); $entry->setAttribute('address', $row['address']); $entry->setAttribute('lat', $row['lat']); $entry->setAttribute('lng', $row['lng']); $entry->setAttribute('type', $row['type']); $entry->setAttribute('distance', $row['distance']); $node->appendChild($entry); } $doc->appendChild($node); // Save the XML echo $doc->saveXML(); echo 'Bytes written: '.$doc->save("markers.xml"); $database->closeConnection(); } catch (PDOException $e) { echo "There is some problem in connection: " . $e->getMessage(); } ?> Okay, I have got this working thank you all! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.