我正在使用this教程,所有内容都适用于PHP 5.4,直到我不得不将我的php更新到版本7.从现在开始查询
$query = sprintf("SELECT id, name, address, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($radius));
不起作用(我知道php 7不支持mysql_query,我需要使用mysqli insted但我尝试了几个选项但它仍然不适合我)。有谁知道怎么做?
答案 0 :(得分:0)
正如你所说的那样使用PHPs,不管怎样都不推荐使用mysql-functions。
从mysqli开始,这是一个很好的观点:
http://php.net/manual/de/book.mysqli.php
例如,您提供的代码可以这样翻译:
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
exit("Failed to connect to MySQL: (".$mysqli->connect_errno.") ".$mysqli->connect_error);
}
$center_lat = $mysqli->real_escape_string($center_lat);
$center_lng = $mysqli->real_escape_string($center_lng);
$radius = $mysqli->real_escape_string($radius);
$res = $mysqli->query("SELECT id, name, address, lat, lng, ( 3959 * acos( cos( radians('".$center_lat."') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('".$center_lng."') ) + sin( radians('".$center_lat."') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '".$radius."' ORDER BY distance LIMIT 0 , 20");
while ($row = $res->fetch_assoc()) {
print_r($row);
}
?>