Android的PHP连接错误? (+ MYSQL) [英] Android php connection error? (+mysql)
问题描述
我想从Android应用程序的位置(经度,纬度),并使用PHP然后10米半径范围内提取数据插入到数据库中。
问题是,当我测试使用的智能手机(本地测试OK)在code,数据插入不正确。表'usergps'有3列(名称,经纬度),我试code后,(0,0)插入。
您可以检查以下PHP code?
如果它是正确的,我想我应该检查Java code。
< PHP
如果(使用isset($ _ POST ['名'])及和放大器;!$ _ POST ['名'] =''){
require_once(包括/ db_info.php);
$ S = mysql_connect($ SERV,$ USER,$ PASS)或死亡(无法连接到MySQL);
mysql_select_db($ DBNM);
$纬度= $ _ POST ['纬度'];
$ LON = $ _ POST ['LON'];
$名称= $ _ POST ['名']; $结果= mysql_query(SELECT * FROM usergps WHERE name ='$名字。'); $行= mysql_num_rows($结果); 如果($行== 0){
的mysql_query(INSERT INTO usergps(名称,纬度,经度)
VALUES
('。$名字。,$纬度。','$ LON。')); }
其他{
的mysql_query(更新usergps组纬度='WHERE name ='$名字。'$纬度。');
的mysql_query(更新usergps SET东经=$ LON。其中name ='$名字。');
}
$查询=的mysql_query(选择
*
(6371 * ACOS(COS(弧度('。$拉丁语'))* COS(弧度(纬度))* COS(弧度(经度)
- 弧度('。$ LON'))+ SIN('。$纬度'弧度())* SIN(弧度(纬度))))作为距离
从usergps
HAVING距离< = 0.01
ORDER BY距离ASC); 如果(!$查询)死亡(无法运行的问题:mysql_error()); $ numpeople = mysql_num_rows($查询);
回声json_en code($ numpeople);}其他{
$响应[错误] = TRUE;
$响应[ERROR_MSG] =所需的参数'名'缺失!;
回声json_en code($响应);
}
?>
请尽量呼应你从Android应用获取数据:$纬度,经度$要检查是否张贴正确性与否
。I'm trying to get location(latitude, longitude) from android application and insert into database using php then extract data within a 10m radius.
The Problem is, when I test the code using smartphone (local test is OK), data is not correctly inserted. Table 'usergps' has 3 columns (name, latitude, longitude) and after I test the code, ( , 0, 0) is inserted.
Can you check the php code below? If it is correct, I think I should check the java code.
<?php
if(isset($_POST['name']) && $_POST['name'] != '') {
require_once("include/db_info.php");
$s=mysql_connect($SERV,$USER,$PASS) or die("fail to connect to mysql");
mysql_select_db($DBNM);
$lat = $_POST['lat'];
$lon = $_POST['lon'];
$name = $_POST['name'];
$result= mysql_query("SELECT * FROM usergps WHERE name = '".$name."'");
$row = mysql_num_rows($result);
if($row == 0) {
mysql_query("INSERT INTO usergps (name,latitude,longitude)
VALUES
('".$name."', '".$lat."', '".$lon."')");
}
else {
mysql_query("UPDATE usergps SET latitude = '".$lat."' WHERE name = '".$name."'");
mysql_query("UPDATE usergps SET longitude = '".$lon."' WHERE name = '".$name."'");
}
$query = mysql_query("SELECT
*,
( 6371 * acos( cos( radians('".$lat."') ) * cos( radians( latitude ) ) * cos( radians( longitude )
- radians('".$lon."') ) + sin( radians('".$lat."') ) * sin( radians( latitude ) ) ) ) AS distance
FROM usergps
HAVING distance <= 0.01
ORDER BY distance ASC");
if(!$query) die ('Unable to run query:'.mysql_error());
$numpeople = mysql_num_rows($query);
echo json_encode($numpeople);
} else {
$response["error"] = TRUE;
$response["error_msg"]= "Required parameter 'name' is missing!";
echo json_encode($response);
}
?>
Please try to echo the data you get from Android app: $lat, $lon to check whether it posts correct data or not.
这篇关于Android的PHP连接错误? (+ MYSQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!