首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Php Read SQL查询-选择2个值-对特定行值运行一些数学运算将值添加到新列?

Php Read SQL查询-选择2个值-对特定行值运行一些数学运算将值添加到新列?
EN

Stack Overflow用户
提问于 2012-04-27 05:14:19
回答 2查看 368关注 0票数 0

问题:我有一个我调用的SQL查询。我希望能够将其放入一个数组中( tbh我现在这样做了),然后从每一行中拉出一个特定值,运行一些数学运算(我有这个),然后将这个数学运算的结果添加到查询的末尾(或开头)的值中,然后在相同的数组或新的数组中提供该值。

这是为了计算出构成SQL查询一部分的当前位置,然后计算出每个事件(每行一个)到该位置的距离。

SQL Query ->检查事件位置->运行距离当前位置上的数学到事件->重新编译为数组(SQL Query + Distance),准备json输出。

我要补充的是,我已经成功地在xml输出输出中做到了这一点,但当涉及到操作数组时,我仍然是一个新手……

谢谢

Terran

代码语言:javascript
复制
    // Connect to database server   
$con = mysql_connect($config_databaseServer,$config_databaseUsername,$config_databasePassword) or die(mysql_error());
mysql_select_db($config_databaseName, $con);    

// Access tables    
$sql = "SELECT * FROM " . $config . " WHERE LAT <" . $toplat . " AND LAT > " . $bottomlat . " AND LONG > " . $leftlon . " AND LONG < " . $rightlon . " AND VALIDPERIOD_STARTOFPERIOD < '" . $nowtime . "' AND VALIDPERIOD_ENDOFPERIOD > '" . $nowtime ."')";

// Execute query
$result = mysql_query($sql) or die(mysql_error());

$lata = $latitude;
$lona = $longitude;
    // Need to populate these from each row values in the query
$latb = $sqllat;
$lonb = $sqllong;                   
// need to place this in to a colum at the end of each row
    $distancefromevent = coordDistance($lata, $longa, $latb, $lonb, "m");

    // need to reform all the above so I can carry on and use the code that follows


$responses = array();
if(mysql_num_rows($result)) {
  while($response = mysql_fetch_assoc($result)) {
    $responses[] = array('dataitem'=>array_map('utf8_encode',$response));
  }
}

header('Content-type: application/json');
$json = json_encode(array($config_datexdeftype=>$responses));

$callback = $_GET[callback];
echo $callback . '(' . $json . ')';

};

return TRUE;
EN

回答 2

Stack Overflow用户

发布于 2012-04-27 05:25:36

想要使用SQL查询计算位置(纬度、经度)之间的距离吗?

如果是,则是这样的:https://developers.google.com/maps/articles/phpsqlsearch?hl=hu-HU

票数 0
EN

Stack Overflow用户

发布于 2012-04-27 05:41:06

这应该是可行的。

请注意,为了提高可读性,一些较长的代码已替换为[...]

代码语言:javascript
复制
// Connect to database server   
$con = mysql_connect([...]) or die(mysql_error());
mysql_select_db($config_databaseName, $con);    

// Access tables    
$sql = "SELECT * FROM " . $config . " WHERE [...]";

// Execute query
$result = mysql_query($sql) or die(mysql_error());

// Prepare the loop
$lata = $latitude;
$lona = $longitude;
$responses = array();

// Loop over each records (stay correct even if there is no record)
while($response = mysql_fetch_assoc($result)) {
    // we assume that latitude and longitude are given by
    //  columns 'lat' and 'lon' in the SQL query.
    $latb = $response['lat']; 
    $lonb = $response['lon'];
    $distancefromevent = coordDistance($lata, $longa, $latb, $lonb, "m");
    $response['distance'] = $distancefromevent;
    $responses[] = array('dataitem'=>array_map('utf8_encode',$response));
}

header('Content-type: application/json');
$json = json_encode(array($config_datexdeftype=>$responses));

$callback = $_GET[callback];
echo $callback . '(' . $json . ')';

return TRUE;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10341529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档