1

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]}%'");
5
  • You need to use parenthesis to wrap your logic Commented Aug 11, 2017 at 4:58
  • @SaadSuri Do you mean like parenthesis around the two BETWEEN statements? Commented Aug 11, 2017 at 5:00
  • Yes you need to evaluate them like your desired result. 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]}%' Commented Aug 11, 2017 at 5:01
  • Learn how to use parameters. Don't put values into quests strings, it's dangerous. Also mysql_ functions have been deprecated a long time ago, switch to mysqli or PDO Commented Aug 11, 2017 at 5:02
  • And yes @SamiKuhmonen is right you need to use mysqli or pdo. mysql_ is deprecated and do this with a parameter. Commented Aug 11, 2017 at 5:05

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.