PHP PDO在非对象上调用成员函数execute()

时间:2013-11-25 17:20:55

标签: php mysql database pdo

所以我正在使用预准备语句来处理从我的cordova应用程序到数据库的JSON调用。

现在,我正在尝试根据POST经度和经度获取附近的地点,但准备好的声明无效。

当我将变量设置为仅测试时,我收到Call to a member function execute() on a non object错误。

我不知道我做错了什么!

    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);

此外,我var_dump()修改了$lat $lon$miles个变量,如下所示。

lat - float(42.356742653104)
lon - float(-71.137704737551)
miles - int(5)

1 个答案:

答案 0 :(得分: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');

query似乎有错误,由于syntax validation失败ORDER BY distance 5错误,它在执行之前就失败了。

它应该

ORDER BY distance ASC

ORDER BY distance DESC