简体   繁体   中英

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. This is the code that is working for me before using 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.

$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. Your alternatives are mysqli_* functions, or to use 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_ .

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. There is a good tutorial on using PHP PDO here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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