简体   繁体   English

PHP Mysql Select 查询与 BETWEEN

[英]PHP Mysql Select query with BETWEEN

I am trying to use two query using between but when I add the between code it does not work.我正在尝试使用两个查询使用 between 但是当我添加 between 代码时它不起作用。 This is the code that is working for me before using BETWEEN.这是在使用 BETWEEN 之前对我有用的代码。

$result = mysql_query("SELECT name , liking , id , address , description FROM locations WHERE liking LIKE '%{$likeArray[$newCount]}%'");

This is the code with BETWEEN that is not working.这是 BETWEEN 不起作用的代码。

$result = mysql_query("SELECT name , liking , id , address , description FROM locations WHERE ‘long’ BETWEEN ‘$Slong’ AND ‘$Blong’ AND lat BETWEEN ‘$Slat’ AND ‘$Blat’ AND liking LIKE '%{$likeArray[$newCount]}%'");

You have a few issues here.你这里有几个问题。

First, I will say, do not use mysql_* functions, they are deprecated in 5.5+ and removed in 7.0.首先,我要说,不要使用 mysql_* 函数,它们在 5.5+ 中被弃用,并在 7.0 中被删除。 Your alternatives are mysqli_* functions, or to use PDO.您的替代方案是 mysqli_* 函数,或者使用 PDO。 I prefer PDO, but I can see why using mysqli_* would be handy, as you could do a text search for mysql_ and replace with mysqli_ .我更喜欢 PDO,但我明白为什么使用 mysqli_* 会很方便,因为您可以对mysql_进行文本搜索并替换为mysqli_

Now, on to your problem.现在,解决您的问题。 You are using back- and forward-ticks for everything, where you should use back-ticks and single quote marks.您对所有内容都使用反引号和正引号,您应该在此处使用反引号和单引号。 Column names should be enclosed in backticks (`) and text should be encolsed in single quotation marks ('), like below:列名应该用反引号 (`) 括起来,文本应该用单引号 (') 括起来,如下所示:

$result = mysqli_query(
    "SELECT name , liking , id , address , description FROM locations WHERE `long` BETWEEN '$Slong' AND '$Blong' AND `lat` BETWEEN '$Slat' AND '$Blat' AND `liking` LIKE '%{$likeArray[$newCount]}%'"
);

EDIT: Also, you are wide open to SQLi (SQL injection) - using PDOs and Prepared Statements will eliminate this threat.编辑:此外,您对 SQLi(SQL 注入)持开放态度 - 使用 PDO 和准备好的语句将消除这种威胁。 There is a good tutorial on using PHP PDO here .有使用PHP PDO一个很好的教程在这里

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

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