-1

I want to display the nearest location around me by the following code

<?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";
                }
              ?>        

When i run this code following error will display

Notice: Trying to get property of non-object in C:\xampp\htdocs\college\index.php on line 56 Colleges Not Found Please add

If I remove $lat and $long, then keep the value on query it works. What is my mistake please suggest me. Thank you

ulferts
  • 2,187
  • 12
  • 19

2 Answers2

0

change your query as below. change ' to " otherwise value will not interpreted:

$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?

B. Desai
  • 16,414
  • 5
  • 26
  • 47
0

I think, that is because, value $lat and $long is treated as string. You could better use prepared statement and bind these variables.

$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);
Ravi
  • 30,829
  • 42
  • 119
  • 173
  • The MySQLi method `bind_param()` isn't like PDOs. `bind_param()` takes all the values in a single call. http://php.net/manual/en/mysqli-stmt.bind-param.php - but indeed, prepared statements is the way to go! – Qirel Sep 24 '17 at 13:11
  • 1
    @Qirel Thanks for pointing out. Corrected. – Ravi Sep 24 '17 at 13:36