1

So I'm using prepared statements to handle my JSON call from my cordova app to a database.

Right now, I'm trying to get nearby locations based on POSTed latitude and longitude, but the prepared statement isn't working.

When I set the variables to just test, I'm getting a Call to a member function execute() on a non object error.

I don't know what I'm doing wrong!

    error_reporting(E_ALL);

    $lat = 42.35674265310388;
    $lon = -71.13770473755070;
    $miles = 5;
    //$lat = $_POST["lat"];
    //$lon = $_POST["lon"];
    $data = array();

    $sth = $mysqli->prepare('SELECT *, (3959 * acos(cos(radians(?)) * cos(radians(lat)) * cos(radians(lon) - radians(?)) + sin(radians(?)) * sin(radians(lat)))) AS distance FROM members WHERE (3959 * acos(cos(radians(?)) * cos(radians(lat)) * cos(radians(lon) - radians(?)) + sin(radians(?)) * sin(radians(lat)))) < ? ORDER BY distance 5');
    $sth->execute(array($lat, $lon, $lat, $lat, $lon, $lat, $miles));
    $row = $sth->fetch();

    $data["markers"][] = array
    (
        "lat" => $row['lat'],
        "lon" => $row['lon']
    );

    print_r($data);

    echo json_encode($data);

Also, I var_dump()ed the $lat $lon and $miles variables, shown below.

lat - float(42.356742653104)
lon - float(-71.137704737551)
miles - int(5)
3
  • 1
    you've tagged this PDO yet you've called your database variable $mysqli? What do your database connection lines look like? Commented Nov 25, 2013 at 17:23
  • 2
    Quite funny to see this one after stackoverflow.com/questions/18321504/… Commented Nov 25, 2013 at 17:38
  • @YourCommonSense I changed everything. Also, novocaine88, I didn't change the variable name, but it is being called via PDO. Idk. Didn't change it. Commented Nov 27, 2013 at 16:50

1 Answer 1

3
$sth = $mysqli->prepare('SELECT *, (3959 * acos(cos(radians(?)) * cos(radians(lat)) * cos(radians(lon) - radians(?)) + sin(radians(?)) * sin(radians(lat)))) AS distance FROM members WHERE (3959 * acos(cos(radians(?)) * cos(radians(lat)) * cos(radians(lon) - radians(?)) + sin(radians(?)) * sin(radians(lat)))) < ? ORDER BY distance 5');

There seems to be an error in the query, it fails on preparation before it is even executed due to syntax validation failure ORDER BY distance 5 which is wrong.

It should be either

ORDER BY distance ASC

OR

ORDER BY distance DESC
Sign up to request clarification or add additional context in comments.

4 Comments

@Downvoter: Can someone add a comment so that i can correct myself.
Also, when I var_dump the query, the response is string(308) followed by the query itself. Also, when I add the bind_param before executing, I get the same error, but the execute() becomes bind_param() I've seen it with the array in the execute() before, and has worked elsewhere in examples that weren't mine. Also, I have seen the bind_param() way you're saying.
Looks like he wants an order, and a limit...So ORDER ASC or ORDER DESC are fine, and add limit 5 after.
Fixing the ORDER BY has fixed it. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.