Fellow Monks,

I have been playing with the handy Geo::Distance module, however I noticed some abnormalities using the 'closest' function for certain lat/lon data when doing DB lookups...

I couldn't work out why this was wrong and looked a bit further, simply put this 'closest' function creates some simple sql to fine destinations within a certain range of lat/lon

However I noticed this info was wrong, however when I did the same sql directly on the DB it did what I expected. Further testing showed that it was only incorrect when placeholders were used, and I think its related to using negative numbers in the logic. Take a look at the following code snippet

y $lon = -0.232323127347249; my $sql = qq| SELECT lon,lat,location_id FROM lat_long WHERE lon>= $lon|; my $sth = $sp->dbh->prepare($sql); $sth->execute(); print STDERR "Rows not using placeholders ".$sth->rows."\n"; $sql = qq| SELECT lon,lat,location_id FROM lat_long WHERE lon >= ?|; $sth = $sp->dbh->prepare($sql); $sth->execute($lon); print STDERR "Rows using placeholders ".$sth->rows."\n";

I would have expected that the 2 SQL would return exactly the same number of results but it doesn't !! see below

Rows not using placeholders 5488 Rows using placeholders 12888

Maybe I am missing something obvious but I do not see how this could happen ? any ideas how to debug further ?

Update : Adding some sample data

__DATA__ insert into lat_long (location_id, lat, lon) values(44977, 48.3547, 11 +.7875); insert into lat_long (location_id, lat, lon) values(53280, 48.1369, 11 +.5456); insert into lat_long (location_id, lat, lon) values(53281, 48.1394, 11 +.5454); insert into lat_long (location_id, lat, lon) values(53282, 47.6869, 11 +.7609); insert into lat_long (location_id, lat, lon) values(53283, 48.3547, 11 +.7875); insert into lat_long (location_id, lat, lon) values(60105, 48.3547, 11 +.7875); insert into lat_long (location_id, lat, lon) values(80766, 48.3507, 11 +.7328); insert into lat_long (location_id, lat, lon) values(71776, 48.1435, 11 +.5574); insert into lat_long (location_id, lat, lon) values(65883, 48.1269, 11 +.6363); insert into lat_long (location_id, lat, lon) values(65884, 48.1515, 11 +.6178);

In reply to Placeholders and Negative Numbers.. by ropey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.