duanmorong9597 2015-06-23 01:58 采纳率: 100%
浏览 27
已采纳

如何在PHP PDO中基于经度,纬度和半径从MYSQL中提取数据?

I'm developing an application where it needs to show results based on the person longitude, latitude and user selected radius, then return the result in Json array format json_encode();

I have 3 files in my project as sdbconnection.php, manipulate.php and pull.php. Below I have explained how exactly each file works.

sdbconnection.php is my server and database connection.

//MySQL Database Connection Credential
class connection {
    public static $host = "XXXXX";
    public static $user = "XXXXXXX";
    public static $password = "XXXXXXX";
    public static $database = "XXXXXXX";
    public static $port = "XXXXX";
}

manipulate.php is how I exccute my SQL query and return the result. I have tried to figure out how to send requests from google tutorial, but i believe that I'm missing something since they are not using the PDO connection.

public function pullResults($longitude, $latitude, $radius){
    $retunValue = array();
    $sql = "SELECT title, latitude, longitude, ( 3959 * acos( cos( radians(:lat) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(:lng) ) + sin( radians(:lat) ) * sin( radians( latitude ) ) ) ) AS distance FROM posts HAVING distance < :radius ORDER BY distance LIMIT 0 , 20";

    $result = $this->connection->query($sql);
        if ($result != null && (mysqli_num_rows($result) >= 1)) {
            $row =  $result->fetch_array(MYSQLI_ASSOC);
            if (!empty($row)) {
                $retunValue = $row;
            }
        }

    return $retunValue;
}

pull.php is where users request to execute their respond and when i try it in browser, I get Blank page.

//Database Connection and Query Manipulation.
require("handles/sdbconnection.php");
require("handles/manipulate.php");

// Get parameters from URL
$latitude = $_GET["latitude"];
$longitude= $_GET["longitude"];
$radius = $_GET["radius"];
$category = htmlentities($_GET["category"]);

//Connect to database
$databaseAccess = new manipulate();
$databaseAccess->openConnection();

//Get the Result
$databaseAccess->pullResults($longitude, $latitude, $radius);

//Close Connection After Data Pulled
$databaseAccess->closeConnection();

Also, if you see, I'm trying to be more specific in my MYSQL query by retrieving data by providing multiple categories ($category = htmlentities($_GET["category"]);), I really appreciate if you show me how to do that as well.

Procedure: enter image description here

  • 写回答

1条回答 默认 最新

  • douxiao0400 2015-06-23 17:14
    关注

    "PDO" and "mysqli_*" are two separate APIs. Do not mix them.

    pullResults has return $retunValue;, yet the call to it throws away the return value.

    $string = json_encode($retunValue);
    

    will product a string, that you can echo into the result.

    Is the "platform" expecting HTML output? Or just JSON text? If the latter, you need this as the first statement that emits output:

    header('Content-Type: application/json');
    

    Bottom line: This might be all you need:

    header('Content-Type: application/json');
    echo json_encode($retunValue);
    

    (I have not checked for other issues.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀