2017-09-24 91 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