您好我正在尝试从他们的IP地址找到我的用户所在的城市,以便能够在我的数据库中显示靠近他们/他们所在区域的用户列表。
我一直在看这个api:
http://ipinfodb.com/ip_location_api.php
我已经注册并获得了api密钥,但我不知道从哪里开始。以下是他们用于获取用户位置的示例代码:
<?php
include('ip2locationlite.class.php');
//Load the class
$ipLite = new ip2location_lite;
$ipLite->setKey('<api key>');
//Get errors and locations
$locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
$errors = $ipLite->getError();
//Getting the result
echo "<p>\n";
echo "<strong>First result</strong><br />\n";
if (!empty($locations) && is_array($locations)) {
foreach ($locations as $field => $val) {
echo $field . ' : ' . $val . "<br />\n";
}
}
echo "</p>\n";
//Show errors
echo "<p>\n";
echo "<strong>Dump of all errors</strong><br />\n";
if (!empty($errors) && is_array($errors)) {
foreach ($errors as $error) {
echo var_dump($error) . "<br /><br />\n";
}
} else {
echo "No errors" . "<br />\n";
}
echo "</p>\n";
?>
但是我现在如何设置一个mysql查询,该查询说明用户位置是否为london,用户是否在数据库中列为london,然后将这些显示给用户?
如果有人能提供帮助,我真的很感激在这里推进正确的方向,谢谢。
答案 0 :(得分:0)
这是一个例子(不知道你的数据库结构和未经测试;)。将其插入到foreach($ locations as $ field =&gt; $ val)循环中,如下所示:
foreach ($locations as $field => $val) {
echo $field . ' : ' . $val . "<br />\n";
$mysqli = new mysqli('<mysql server>', '<mysql user>', '<mysql password>', '<database>');
if(mysqli_connect_errno()) {
echo 'Error: Unable to connect to Database; ';
} else {
if ($stmt_evt = $mysqli -> prepare('SELECT <other_users>, ... FROM <your table> WHERE <user_location> LIKE ? ORDER BY <whatever>')) {
$stmt_evt -> bind_param('s', $val);
$stmt_evt -> execute();
$stmt_evt -> bind_result($<other_users>, $...);
while ($stmt_evt->fetch()) {
echo 'Other users in your area: '.$<other_users>;
}
} else {
echo 'Error: Unable to prepare Event statement.';
}
}
}