2017-09-24 88 views
-1

的財產我想下面的代碼獲取有關試圖讓非對象

<?php 
    $lat = "27.7350758"; 
    $long = "85.3102946"; 

    $result = $conn->query('SELECT * , (3956 * 2 * ASIN(SQRT(POWER(SIN(($lat - college_lat) * pi()/180/2), 2) +COS($lat * pi()/180) * COS(college_lat * pi()/180) * POWER(SIN(($long - college_lon) * pi()/180/2), 2)))) as distance from colleges having distance <= 10 order by distance'); 

       if ($result->num_rows>0) { 
        // output data of each row 
        while($row = $result->fetch_assoc()) { 
         echo '<div class="category"><div class="cover" style="background-image: url(img/animation.jpg);"><div>'; ?>  
         <td><?php echo $row[ "college_name" ]; ?></td> 
         <?php 
         echo '</div></div><span class="counter ">Courses : <span>' . $row[ "college_courses" ] . '</span></span>'; 
         echo '<span class="counter "> Distance : <span>' . $row[ "college_address" ] . '</span></span>'; 
         echo '<span class="near-you"> <p>'. substr($row[ "college_desc" ], 0 , 100) . '</p> </span></div>'; ?> 
         <a href="single.php?college_id=<?php echo $row['college_id'];?>"> Read More... </a> 

       <?php  
        } 
       } else { 
        echo "Colleges Not Found Please add"; 
       } 
       ?>   

當我運行這段代碼在我身邊,顯示最近的位置下面的錯誤將顯示

通知錯誤:試圖讓非對象的屬性在C:\ XAMPP \ htdocs中\上線大學\的index.php 56所 院校未找到,請添加

如果我刪除$ lat和$ long,然後保持查詢的效果。 我的錯誤請給我建議。謝謝

回答

0

更改您的查詢如下。改變'"否則價值不會解釋:

$result = $conn->query("SELECT * , (3956 * 2 * ASIN(SQRT(POWER(SIN(($lat - college_lat) * pi()/180/2), 2) +COS($lat * pi()/180) * COS(college_lat * pi()/180) * POWER(SIN(($long - college_lon) * pi()/180/2), 2)))) as distance from colleges having distance <= 10 order by distance"); 

What is the difference between single-quoted and double-quoted strings in PHP?

+0

嘗試過但錯誤相同 –

0

我認爲,那是因爲,價值$lat$long作爲字符串處理。你可以更好地使用準備好的語句並綁定這些變量。

$stmt = $mysqli->prepare("SELECT * , (3956 * 2 * ASIN(SQRT(POWER(SIN((? - college_lat) * pi()/180/2), 2) + COS(? * pi()/180) * COS(college_lat * pi()/180) * POWER(SIN((? - college_lon) * pi()/180/2), 2)))) as distance from colleges having distance <= 10 order by distance"); 
$stmt->bind_param('ddd', $lat, $lat, $long); 
+0

MySQLi方法'bind_param()'不像PDO。 'bind_param()'在一次調用中獲取所有的值。 http://php.net/manual/en/mysqli-stmt.bind-param.php - 但的確,準備好的語句是要走的路! – Qirel

+1

@Qirel感謝您指出。糾正。 – Ravi