php - 在 PHP 中从 MySQL 获取 Radius

标签 php mysql syntax-error

所以我有一个 PHP 脚本,它从 MySQL 获取数据,msg_location 使用点空间数据。我试图仅检索 10 英里半径内的数据。以下是我到目前为止所拥有的:

<?php
$lat = $_POST['latitude'];
$long = $_POST['longitude'];
require_once $_SERVER['DOCUMENT_ROOT'].'/assets/php/database/main.php';
$records = $conn->query('SELECT message, X(msg_location), Y(msg_location), loved 
    FROM hidyn_msg WHERE 3963 * ACOS(
    SIN(RADIANS(X(msg_location))) * SIN(RADIANS(X(msg_location))) + COS(RADIANS(X(msg_location)))  * COS(RADIANS(:lat)) * COS(RADIANS(:long) - RADIANS(:long))) <= 10
 ORDER BY msg_date DESC');
    $stmt -> bindparam(':lat', $lat);
    $stmt -> bindparam(':long', $long);
    try{
        $records->execute();
        $results = $records->fetchAll(PDO::FETCH_ASSOC);
        if(count($results) > 0):
            echo json_encode($results);
        endif;
    }catch(Exception $e){
        $stmtError = $stmt->errorInfo();
        $statusMSG= array('error'=>false, 'message' => "We had a issue creating and posting your message. Error: ", $e[0]);
        echo json_encode(statusMSG);
    }
?> 

顶部接收用户的地理位置并将其发布到 php 文件以检索数据。

错误发生在$records->execute();我尝试更改脚本以回显错误,但它没有回显任何内容。 SQL 在 MySQL 控制台中运行并且运行良好。

工作代码: RamRider 的帮助

<?php
$lat = $_POST['latitude'];
$long = $_POST['longitude'];
require_once $_SERVER['DOCUMENT_ROOT'].'/assets/php/database/main.php';
$sql = ('SELECT msg_date, message, loved
    FROM hidyn_msg WHERE
         msg_location = ((ACOS(SIN(:lat * PI() / 180) * SIN(X(msg_location) * PI() / 180) + 
         COS(:lat * PI() / 180) * COS(X(msg_location) * PI() / 180) * COS((:long - Y(msg_location)) * 
         PI() / 180)) * 180 / PI()) * 60 * 1.1515) <= 10
 ORDER BY msg_date DESC');
    $stmt = $conn->prepare($sql);
    $stmt -> bindparam(':lat', $lat);
    $stmt -> bindparam(':long', $long);
    try{
        $results = $stmt->execute();
        $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
        if(count($results) > 0):
            echo json_encode($results);
        endif;
    }catch(Exception $e){
        $stmtError = $stmt->errorInfo();
        $statusMSG= array('error'=>false, 'message' => "We had a issue creating and posting your message. Error: ", $e[0]);
        echo json_encode(statusMSG);
    }
?>

最佳答案

引用我的评论(未经测试),它通常更像是这样的:

<?php

    $lat = $_POST['latitude'];
    $long = $_POST['longitude'];

    require_once $_SERVER['DOCUMENT_ROOT'].'/assets/php/database/main.php';

    $sql='select message, x(msg_location), y(msg_location), loved 
        from hidyn_msg where 3963 * acos(
        sin(radians(x(msg_location))) * sin(radians(x(msg_location))) + cos(radians(x(msg_location)))  * cos(radians(:lat)) * cos(radians(:long) - radians(:long))) <= 10
        order by msg_date desc';

        $stmt = $conn->prepare( $sql );
        $stmt->bindParam(':lat', $lat);
        $stmt->bindParam(':long', $long);

        try{
            $stmt->execute();
            $results = $stmt->fetchAll( PDO::FETCH_ASSOC );
            if(count($results) > 0):
                echo json_encode($results);
            endif;
        }catch(Exception $e){
            $stmtError = $stmt->errorInfo();
            $statusMSG= array('error'=>false, 'message' => "We had a issue creating and posting your message. Error: ", $e[0]);
            echo json_encode(statusMSG);
        }
?> 

关于php - 在 PHP 中从 MySQL 获取 Radius,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48855450/

相关文章:

php - 是否可以组合这 3 个 mySQL 查询?

MySQL 数据库为什么 Õ 在搜索时返回为 O

javascript - ReactJS:未捕获的语法错误:意外的 token (

php - 未经授权的谷歌访问 token

php - NGINX 的 apache_request_headers 函数

mysql - 多线程 Delphi 数据库应用程序因大量数据而失败

php mailgun 错误意外)

debugging - 如何更改 AwesomeWM 标签名称?

php - 如何根据id显示两个表中的数据

Mysql 4转5查询转换——加入头痛