Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中从MySQL获取Radius_Php_Mysql_Syntax Error - Fatal编程技术网

在PHP中从MySQL获取Radius

在PHP中从MySQL获取Radius,php,mysql,syntax-error,Php,Mysql,Syntax Error,所以我有一个PHP脚本,它从MySQL获取数据,msg_位置使用点空间数据。我试图只检索10英里半径范围内的数据。以下是我目前的情况: <?php $lat = $_POST['latitude']; $long = $_POST['longitude']; require_once $_SERVER['DOCUMENT_ROOT'].'/assets/php/database/main.php'; $records = $conn->query('SELECT message, X

所以我有一个PHP脚本,它从MySQL获取数据,msg_位置使用点空间数据。我试图只检索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控制台中工作,并且工作得非常好

工作代码: 拉姆莱德的帮助

<?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);
        }
?> 

如果您使用的Mysql版本大于或等于5.7,那么您可以使用ST_DISTANCE_SPHERE

mysql>SET@pt1=ST_GeomFromText'POINT0'; mysql>SET@pt2=ST_GeomFromText'POINT180 0'; mysql>选择ST_距离_Sphere@pt1,@pt2

输出

ST_Distance_Sphere(@pt1, @pt2) 
 20015042.813723423

以米为单位的距离

您不应该查询然后执行-要执行,您还需要首先准备sqlstatement@RamRaider我在某个地方读到关于PDO的文章,你需要使用execute函数否,你可以根据你的情况选择不同的查询数据库的方法。当需要用户输入时,准备语句,然后执行——使用静态sql查询,您可以选择查询方法——但这两种方法都不能像上面所说的那样都起作用,所以您必须对代码进行一些编辑,但正如您所说的,您没有对其进行测试。我要在我的代码中添加补丁,向人们展示我的确切技术。。。再次感谢你,因为我正要用拳头穿过电脑,很高兴它能帮上忙——我注意到你在修改过的代码中调用了execute方法两次。同样-应该不需要执行/查询两次。哎哟。我的b.解决了这个问题