0

I have this SQL code

$sql = 'SELECT * FROM images WHERE 
(artist LIKE "'.$artistsearch.'%") 
AND (code LIKE "'.$idsearch.'%") 
AND (name LIKE "'.$namesearch.'%") 
AND (price BETWEEN '.$minprice.' AND '.$maxprice.')';

And I get the error

Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND )' at line 5

However, running the code

SELECT * 
FROM images WHERE artist LIKE '%' 
AND code LIKE '%' 
AND name LIKE '%' 
AND price BETWEEN 0 AND 1000

Works fine in MySQL.

I've tried switching around ' and " and it doesn't work.

Can anyone help me figure out the problem?

7
  • 1
    Just print out the value of the $sql variable and see what's wrong. Commented Jan 16, 2015 at 17:25
  • Are you sure that's the line that's giving you the error? The error message suggests that your SQL includes AND ), and that string doesn't appear in that code Commented Jan 16, 2015 at 17:26
  • One of your php variables has value = "". Commented Jan 16, 2015 at 17:27
  • 4
    @andrewsi - it does if $maxprice is empty Commented Jan 16, 2015 at 17:27
  • 1
    The MySQL code you just showed us does not even contain brackets so therefore it cannot be the printout of $sql Commented Jan 16, 2015 at 17:29

2 Answers 2

2

The error says the everything the problem in the last line

AND (price BETWEEN '.$minprice.' AND '.$maxprice.')';

as you can see "AND $maxprice )" part is the problem. Check the $maxprice and be sure it is not empty

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

Comments

0
if (empty($maxprice)) {
   $maxprice = "some_default_number";
}

$sql = 'SELECT * FROM images WHERE 
   (artist LIKE "'.$artistsearch.'%") 
   AND (code LIKE "'.$idsearch.'%") 
   AND (name LIKE "'.$namesearch.'%") 
   AND (price BETWEEN '.$minprice.' AND '.$maxprice.')';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.