PHP设置值时的查询问题?

标签 php mysql sql mamp haversine

快速提问。我有一个适用于我的数据库的 SQL 查询。但是,我确定如何在 PHP 文件中设置参数,以便它适用于我的查询?

有什么想法吗?我在 php 方面设置参数并使我的查询工作时遇到问题。

<?php

/*
 * Following code will list all the products
 */

// array for JSON response
$response = array();

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

set $orig_lat=32.42; 
set $orig_long=-23.550; 
set $bounding_distance=1;

// get all products from products table
$result = mysql_query(" SELECT * ,((ACOS(SIN($orig_lat * PI() / 180) * SIN(`latitude` * PI() /
180) + COS($orig_lat * PI() / 180) * COS(`latitude` * PI() / 180) *
COS(($orig_long - `longitude`) * PI() / 180)) * 180 / PI()) * 60 *
1.1515) AS `distance` FROM `Location` WHERE ( `latitude` BETWEEN
($orig_lat - $bounding_distance) AND ($orig_lat + $bounding_distance)
AND `longitude` BETWEEN ($orig_long - $bounding_distance) AND
($orig_long + $bounding_distance) ) ORDER BY `distance` ASC limit 1") or die(mysql_error()); 

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["Location"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
       $Location = array();
       $Location["id"] = $row["id"];
       $Location["latitude"] = $row["latitude"];
       $Location["longitude"] = $row["longitude"];



        // push single product into final response array
        array_push($response["Location"], $Location);
    }
    // success
    $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No Profiles found";

    // echo no users JSON
    echo json_encode($response);
}
?>

最佳答案

这是一个简单的语法问题...与查询相对应的值没有很好地注明。在 PHP 文件中,您不能使用 $ 编写设置和声明变量。

原文:

set $orig_lat=32.42; 
set $orig_long=-23.550; 
set $bounding_distance=1;

解决方案:

$orig_lat=32.42; 
$orig_long=-23.550; 
$bounding_distance=1;

关于PHP设置值时的查询问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30126566/

相关文章:

php - PHP 中的长随机字符串生成器

php - 如何使用 PHP 获取 Mysql 数据库中的最后一个字段?

mysql - 用户名 'root' 可以在 mysql 中更改,或者我可以假设总是有一个名为 'root' 的用户拥有所有权限?

mysql - 按 MAX(time) WHERE time <= x 选择最近的 MySQL 行

mysql - 错误代码 : 1066. 不是唯一表/别名问题

c++ - SQL 选择多个 ids firebird

php - 使用 PHP 将数据库行放入数组中?

php - Cakephp从多个表中获取数据

sql - 为什么在检查表是否存在时使用 OBJECT_ID

php - 什么会导致这样的事情? PHP - MYSQL 失败