1

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!

ramr
  • 1,009
  • 1
  • 10
  • 16
  • "Please note that this method requires the mysqlnd driver. Otherwise you will get this error: `Call to undefined method mysqli_stmt::get_result()`" - [Source](http://www.php.net/manual/en/mysqli-stmt.get-result.php) – billyonecan Apr 25 '13 at 08:48
  • Im using the same method to input i believe so, i do think I have it. – uneducatedguy Apr 25 '13 at 08:49
  • This question [here](http://stackoverflow.com/questions/8321096/call-to-undefined-method-mysqli-stmtget-result) might be able to help – billyonecan Apr 25 '13 at 08:52
  • 2
    Shouldn't it be $stmt->get_result($result); not $result =. EDIT: this may actually be wrong so feel free to ignore me :) – Dave Apr 25 '13 at 08:52
  • @Dave You only need to pass in a statement when using it procedurally (ie. `mysqli_stmt_get_result($stmt)`) – billyonecan Apr 25 '13 at 08:57
  • You can pass the variable in, in OO too if your wanting to bind the output to that variable but thats more for returning a single result so you'd then just to echo $result after the fetch. This doesn't work though if you're returning multiple columns. Its basically short hand. look on the php mysqli::prepare page for an example – Dave Apr 25 '13 at 08:59

1 Answers1

1

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 } ?>
Your Common Sense
  • 152,517
  • 33
  • 193
  • 313
  • There's very little difference between the 2 for most users tbh. Mysqli is arguably simplier for a new user to get into PDO has some oddities which can be confusing at first. – Dave Apr 25 '13 at 09:16