简体   繁体   English

MySQL选择参数之间的多个查询,变量不起作用

[英]MySQL Select Query Multiple Between Arguments, Variable not Working PHP

I'm developing an app that, for a given location (coordinate lat/long pair), it displays all entities (pulled from database) located within a given radius from the given starting location. 我正在开发一个针对给定位置(经纬度/经度对)的应用程序,它显示从给定起始位置到给定半径内的所有实体(从数据库中拉出)。

I've tested, and the logic that I'm using to find the nearest locations works great. 我已经测试过,并且我用来查找最近位置的逻辑也很好。 However, in my PHP script to communicate with the MySQL table, I'm running into an issue. 但是,在我的与MySQL表通信的PHP脚本中,我遇到了一个问题。

Here is my query that I am attempting, which gives me no results from the table: 这是我正在尝试的查询,该查询没有给出该表的结果:

$sql = "SELECT * FROM merchants WHERE latitude BETWEEN '$lat1' AND '$lat2' AND longitude BETWEEN '$lon1' AND -90.2745 AND category='$merchCategory'";           

However, from echoing out $lat1, $lat2, $lon1, and $lon2, I know that they contain valid values (that should produce results from the query (FYI: these 4 variables represent a radius square from a given location; latitude and longitude are columns in the table.) 但是,通过回显$ lat1,$ lat2,$ lon1和$ lon2,我知道它们包含有效值(应该从查询中产生结果(仅供参考:这4个变量代表给定位置的半径平方; 纬度经度是表格中的列。)

$lon1= -90.644843447 $lon2= -90.274500553 $lat1= 38.3496784638 $lat2= 38.6395335362

However, this works (just hard-coding in the value, not referencing it w/ the $lon2 variable: 但是, 可行(只是硬编码值,而不用$ lon2变量引用它:

$sql = "SELECT * FROM cashbackengine_merchants WHERE latitude BETWEEN '$lat1' AND '$lat2' AND longitude BETWEEN '$lon1' AND **-90.2745** AND category='$merchCategory'";            

As does this: 就像这样:

$sql = "SELECT * FROM cashbackengine_merchants WHERE latitude BETWEEN '$lat1' AND '$lat2' AND longitude BETWEEN **-90.64484** AND '$lon2' AND category='$merchCategory'";           

Yet no problems with the latitude at all. 但是,纬度完全没有问题。 I feel strongly this has something to do with syntax and likely is something very simple. 我强烈认为这与语法有关,并且可能很简单。 Any help is greatly appreciated! 任何帮助是极大的赞赏!

Try this 尝试这个

$sql = "SELECT * FROM merchants WHERE latitude BETWEEN '$lat1' AND '$lat2' AND longitude BETWEEN $lon1 AND $lon2 AND category='$merchCategory'";

I'm not sure but I think this is because BETWEEN requires two numbers for your case and you're passing it as a string '$lon' . 我不确定,但是我认为这是因为BETWEEN要求输入两个数字,并将其作为字符串'$lon'传递。 And when you hardcode one value, other one is automatically converted to a number value. 当您对一个值进行硬编码时,另一个将自动转换为数字值。

Sidenote: Look into prepared statements (best) or escaping the string, as this opens you to sql injections if lat/lon values are being entered by the user. 旁注:查看准备好的语句(最佳)或转义字符串,因为如果用户正在输入经/纬度值,这将使您可以进行sql注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM