我想通过以下代码显示我周围最近的位置
<?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";
}
?>
当我运行此代码时,将显示以下错误
注意:尝试在第56行的C:\ xampp \ htdocs \ _city \ index.php中获取非对象的属性 未找到大学请添加
如果我删除$ lat和$ long,那么保持查询值可以正常工作。 我的错误是什么,请建议我。谢谢
答案 0 :(得分: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?
答案 1 :(得分: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);