2013-11-25 115 views
1

所以我使用預處理語句來處理從我的cordova應用程序到數據庫的JSON調用。PHP PDO調用一個非對象的成員函數execute()

現在,我試圖根據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

你標記這個PDO可是你卻叫你的數據庫變量'$ mysqli'?你的數據庫連接線是什麼樣的? – Novocaine

+2

非常有趣的看到這一個http://stackoverflow.com/questions/18321504/php-select-with-pdo-call-to-a-member-function-prepare-on-a-non-object-error –

+0

@YourCommonSense我改變了一切。另外,novocaine88,我沒有更改變量名稱,但它是通過PDO調用的。 IDK。沒有改變它。 – copilot0910

回答

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 

OR

ORDER BY distance DESC 
+0

@Downvoter:有人可以添加評論,以便我可以糾正自己。 – Thalaivar

+0

剛剛做過。謝謝。 – copilot0910

+0

另外,當我var_dump查詢時,響應是字符串(308),後面是查詢本身。另外,當我在執行前添加'bind_param'時,我得到了同樣的錯誤,但是'execute()'變成了'bind_param()'我之前在'execute()'中看到了它,並且已經在不是我的例子的其他地方工作。另外,我已經看到了你說的'bind_param()'方式。 – copilot0910

相關問題