mysqli multiple where子句用于两个不同的变量

时间:2013-04-25 08:44:35

标签: php get mysqli fatal-error

我将用户当前的纬度和经度保存为变量$latitude$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
}  
?>

但是我得到了错误Fatal error: Call to undefined method mysqli_stmt::get_result()而我似乎无法解决它为什么会出现!

1 个答案:

答案 0 :(得分:1)

只是退出不可用的mysqli并使用PDO,它有两个优点:整洁且有效。

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