Search code examples
phpgetmysqlifatal-error

mysqli multiple where clause for two different variables


I have users current latitude and longitude saved as the variables $latitude and $longitude. I only want the data from the database to echo out if the latitude and longitude matches the users current latitude and longitude.

$con=mysqli_connect("localhost","db_username","password","db_dbname");
$stmt = $con->prepare('SELECT * FROM test WHERE geoloc1 = ? and geoloc2 = ?');
$stmt->bind_param('ss', $latitude, $longitude);
$stmt->execute();

$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
?>

            <div class="latlon">
                <a><?php echo $row['geoloc1'] ?></a>
            </div>
<?php
}  
?>

yet Im getting the error Fatal error: Call to undefined method mysqli_stmt::get_result() and I can't seem to solve why its coming up!


Solution

  • Just quit that unusable mysqli and use PDO, which has 2 advantages: in is neat and it works.

    $dsn = "mysql:host=localhost;dbname=db_dbname;charset=utf8";
    $opt = array(
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    );
    $con = new PDO($dsn, "db_username","password", $opt);
    
    $stmt = $con->prepare('SELECT * FROM test WHERE geoloc1 = ? and geoloc2 = ?');
    $stmt->execute(array($latitude, $longitude));
    $data = $stmt->fetchAll();
    
    foreach ($data as $row) {
    ?>
          <div class="latlon">
             <a><?php echo $row['geoloc1'] ?></a>
          </div>
    <?php } ?>