George Young
George Young

Reputation: 29

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]}%'");

Upvotes: 1

Views: 2308

Answers (1)

e_i_pi
e_i_pi

Reputation: 4820

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.

Upvotes: 1

Related Questions