Junk after document element

HI, I need your help XML experts,I am have this kind of error when

I run this in my url http://localhost/maps/mymap.php?lat=37&lng=-122&radius=25

XML Parsing Error: junk after document element
Location: http://localhost/maps/mymap.php?lat=37&lng=-122&radius=25
Line Number 2, Column 1:<font size=‘1’><table class=‘xdebug-error xe-warning’ dir=‘ltr’ border=‘1’ cellspacing=‘0’ cellpadding=‘1’>
^

This is the code


<?php

$center_lat = $_GET["lat"];
$center_lng = $_GET["lng"];
$radius = $_GET["radius"];


$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);



try{
    $stringconnection = new PDO("mysql:host=localhost;dbname=mydb", 'root', '');
    $stringconnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $cmd = $stringconnection->prepare("SELECT storename,address,lat,lng ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM tblstore HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
        $center_lat,$center_lng,$radius
    );

    header("Content-type: text/xml");

    while($row = $cmd->fetch(PDO::FETCH_OBJ)){
        $node = $dom->createElement("marker");
        $newnode = $parnode->appendChild($node);

        $newnode->setAttribute("store name", $row->storename);
       $newnode->setAttribute("address", $row->address);
        $newnode->setAttribute("lat", $row->lat);
        $newnode->setAttribute("lng", $row->long);
        $newnode->setAttribute("distance", $row->distance);

    }

    echo $dom->saveXML();


}
catch(PDOException $ex){
    echo $ex->getMessage();
    exit();
}



please help me how to solve this.

Thank you in advance.

Not 100% sure if it’s the error or not (it’s been a while since I’ve done php :shifty:), but it looks to me like you’re passing three arguments ($center_lat, $center_lang, $radius) to the sql statement, but only utiliziing one (the %s)

do i need to remove the %s ?

Okay i have this clear error now
pointing to line 20

Warning: PDO::prepare() expects at most 2 parameters, 4 given in

pointing to line 24

Fatal error: Call to a member function fetch() on a non-object in

Okay i tried to replace the prepare to sprintf


$query = sprintf("SELECT storename,address,lat,lng ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM tblstore HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
        $center_lat,$center_lng,$radius
    );

 $cmd = $stringconnection->prepare($query);
    $cmd->execute();

Then this is the display in my browser.

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<markers/>

Thank you in advance.

Comment out the header statement:

// header(“Content-type: text/xml”);

Depends on what you’re trying to do - it looks like you’re trying to find all records with a value less than the selected radius? If so, you’d change it to something like this:


$cmd = $stringconnection->prepare("SELECT storename, address, lat, lng (3959 * acos(cos(radians(37)) * cos(radians(lat)) * cos(radians(lng) - radians(-122)) + sin(radians(37)) * sin(radians(lat)))) AS distance FROM tblstore HAVING distance < ? ORDER BY distance LIMIT 0 , 20", $radius);

This is the error

Warning: PDO::prepare() expects parameter 2 to be array, string given in

Thank you in advance

I change it sprintf but it only display like i post in #5

so change $radius to array($radius)

note: since it’s been so long since I’ve done active php development (over a year), I’m getting the syntax from: http://php.net/manual/en/pdo.prepared-statements.php

I did not use prepare,i used sprintf

and?

If you’re getting the same response as in post #5, did you do what I suggested in post #6?

Hi dave, which code that i am lacking ?..i am using PDO,the example is mysql.so how do this in PDO ?..

Thank you in advance.

Hi jemz,

Did you manage to get this sorted? Just wondering.

I believe all your problems stem from the fact that the string being passed to sprintf as the first argument only contains a single placeholder. However, you seem to be passing 3 more arguments. I can’t recall if that is legal with sprintf but I do know that regardless of whether is runs without a fatal error the $center_lat is going to replace that single placeholder. However, given the variable names I assume that $radius should actually replace %s in the having clause. So the question comes down to whether you need to even be passing $center_lat and $center_lng because they don’t look as if they are used anywhere in the query or should they be? Also be cautious of SQL injection now that you are using sprintf instead of a prepared statement using variable binding for the input arguments.It would probably be in your best interest to switch back to using a prepared statement.

I still getting problem i tried dave post# 7 but the outputs is like this

<?xml version="1.0"?>
<markers/>

here is the code


$cmd = $stringconnection->prepare("SELECT storename, address, lat, lng (3959 * acos(cos(radians(37)) * cos(radians(lat)) * cos(radians(lng) - radians(-122)) + sin(radians(37)) * sin(radians(lat)))) AS distance FROM tblstore HAVING distance < ? ORDER BY distance LIMIT 0 , 20");

$cmd->execute(array($radius));

Hi,

Is this what you mean ?


$cmd = $stringconnection->prepare("SELECT storename, address, lat, lng (3959 * acos(cos(radians(37)) * cos(radians(lat)) * cos(radians(lng) - radians(-122)) + sin(radians(37)) * sin(radians(lat)))) AS distance FROM tblstore HAVING distance < ? ORDER BY distance LIMIT 0 , 20");

$cmd->execute(array($radius));

if it is the output of this would be like this.

<?xml version="1.0"?>
<markers/>

Ok, I’m not sure how much I’ll be able to help you, but I’ll give it a go.
Could you start over by telling me what you are trying to do in high level terms (e.g. I am trying to read abc from a database and do xyz with it).
Then, could your paste current code again (I believe it has changed throughout the thread), as well as whatever error message you are receiving.

Hi pullo,

I have latitude and longitude in each stores and i stored it to my database,now what i want to do is to find the closest 20 locations that are within a radius of 25 miles to the 37, -122 coordinate.


<?php
header("Content-type: text/xml");
$center_lat = $_GET["lat"];
$center_lng = $_GET["lng"];
$radius = $_GET["radius"];


$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);




try{
    $cnn = new PDO("mysql:host=$myhost;dbname=$mydb", '$user', '');
    $cnn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $cmmand =$cnn->prepare("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM tblstore HAVING distance < '%s    ' ORDER BY distance LIMIT 0 , 20" );


     $cmmand->execute(array($center_lat,$center_lng,$center_lat,$radius));




    while ($row =  $cmmand->fetch(PDO::FETCH_OBJ)){
        $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);

    }

  echo $dom->saveXML();



}
catch(PDOException $ex){
    echo $ex->getMessage();

}


when i run the page it only display like this.

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<markers/>

supposedly i am expecting output like this


<?xml version="1.0"?>
<markers><marker name="Round Table Pizza: Mountain View" address="570 N Shoreline Blvd, Mountain View, CA" lat="37.402653" lng="-122.079353" distance="0.38091455044131"/>
<marker name="Kapp's Pizza Bar & Grill" address="191 Castro St, Mountain
     View, CA" lat="37.393887" lng="-122.078918" distance="0.5596115438175"/>
<marker name="Amici's East Coast Pizzeria" address="790 Castro St, Mountain View, CA" lat="37.387138" lng="-122.083237" distance="1.0796074495809"/>
<marker name="Frankie Johnnie & Luigo Too" address="939 W El Camino Real, Mountain View, CA" lat="37.386337" lng="-122.085823" distance="1.2044231336188"/>
<marker name="Tony & Alba's Pizza & Pasta" address="619 Escuela Ave, Mountain View, CA" lat="37.394012" lng="-122.095528" distance="1.3156538737837"/><marker name="Round Table Pizza: Sunnyvale-Mary-Central Expy" address="415 N Mary Ave, Sunnyvale, CA" lat="37.390038" lng="-122.042030" distance="1.84565061776"/>
<marker name="Oregano's Wood-Fired Pizza" address="4546 El Camino Real, Los Altos, CA" lat="37.401726" lng="-122.114647" distance="2.2887425990519"/>
...
</markers>


Hope this helps.:slight_smile:

Hey jemz,

As far as I know, you can’t use %s as a placeholder in a PDO prepared statement… you need to use either named placeholders (i.e. :value) or a ?, so you would need to alter your query like this:


$cmmand = $cnn->prepare("SELECT address, name, lat, lng,
    ( 3959 * acos( cos( radians( ? ) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( lat ) ) ) ) AS distance
    FROM tblstore HAVING distance < ? ORDER BY distance LIMIT 0 , 20");

http://www.php.net/manual/en/pdo.prepared-statements.php