SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Post junk after document element

    HI, I need your help XML experts,I am have this kind of error when
    XML Parsing Error: junk after document element
    Location: http://localhost/maps/mymap.php?lat=...-122&radius=25
    Line Number 2, Column 1font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
    ^

    This is the code

    PHP 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_ERRMODEPDO::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.

  2. #2
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,280
    Mentioned
    121 Post(s)
    Tagged
    1 Thread(s)
    Not 100% sure if it's the error or not (it's been a while since I've done php ), 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)
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  3. #3
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DaveMaxwell View Post
    Not 100% sure if it's the error or not (it's been a while since I've done php ), 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 ?

  4. #4
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DaveMaxwell View Post
    Not 100% sure if it's the error or not (it's been a while since I've done php ), 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)

    Okay i have this clear error now
    pointing to line 20
    Warning: PDO:repare() 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

  5. #5
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Okay i tried to replace the prepare to sprintf

    PHP Code:
    $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.

  6. #6
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,280
    Mentioned
    121 Post(s)
    Tagged
    1 Thread(s)
    Comment out the header statement:

    // header("Content-type: text/xml");
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  7. #7
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,280
    Mentioned
    121 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by jemz View Post
    do i need to remove the %s ?
    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:

    Code PHP:
    $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);
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  8. #8
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DaveMaxwell View Post
    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:

    Code PHP:
    $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:repare() expects parameter 2 to be array, string given in
    Thank you in advance

  9. #9
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I change it sprintf but it only display like i post in #5

  10. #10
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,280
    Mentioned
    121 Post(s)
    Tagged
    1 Thread(s)
    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
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  11. #11
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I did not use prepare,i used sprintf

  12. #12
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,280
    Mentioned
    121 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by jemz View Post
    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?
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  13. #13
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DaveMaxwell View Post
    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.

  14. #14
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,999
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi jemz,

    Did you manage to get this sorted? Just wondering.

  15. #15
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,182
    Mentioned
    16 Post(s)
    Tagged
    4 Thread(s)
    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.
    The only code I hate more than my own is everyone else's.

  16. #16
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Hi jemz,

    Did you manage to get this sorted? Just wondering.
    I still getting problem i tried dave post# 7 but the outputs is like this

    Code:
    <?xml version="1.0"?>
    <markers/>
    here is the code
    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));

  17. #17
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    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.
    Hi,

    Is this what you mean ?

    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));
    if it is the output of this would be like this.

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

  18. #18
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,999
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    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.

  19. #19
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    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 Code:
    <?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_ERRMODEPDO::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


    HTML Code:
    <?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.

  20. #20
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,437
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    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:

    PHP Code:
    $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.pre...statements.php

  21. #21
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    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:

    PHP Code:
    $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.pre...statements.php
    Hi fretburner, I tried already but still having this ouput

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

  22. #22
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,437
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    It sounds like your query might not be returning any results.. have you tried entering the query into phpMyAdmin or something similar to check that it actually returns something for the given input?

  23. #23
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Nope, I don't know how to test this in phpmyadmin if this really works.

  24. #24
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,437
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Copy and paste the SQL of your query into phpmyadmin, but replace the ? placeholders with the actual values that you've been testing your script with. That way, you'll find out if the query is actually returning any data.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •