5

I am trying to do a select query on MySQL with phpMyAdmin or PHP with PDO.

SELECT 'uid' FROM 'clusters' WHERE 'lat'<='47.21125' AND 'lat'>='39.21125' AND 'lng'<='32.90243' AND 'lng'>='22.90243'

However, phpMyAdmin says:

#1064 - 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 ''clusters' WHERE 'lat'<='47.21125' AND 'lat'>='39.21125' AND 'lng'<='32.90243' A' at line 1

What is wrong with it?

1

1 Answer 1

10

'' creates a string literal in MySQL, so your query is selecting the literal "uid" from the literal "clusters," which is invalid. Use backtics (or nothing)

SELECT Uid FROM clusters WHERE lat <= 47.21125 AND lat >= 39.21125
AND lng >= 22.90243
Sign up to request clarification or add additional context in comments.

5 Comments

i just want to say, is not me who give a down vote. however, you should not using quote for the double value as well.
@ajreal I don't think that it makes a difference, but I edited my answer to remove them
the quote will force mysql to perform unnecessary casting, which by no mean ... the index could be ignored (however, the SQL by OP is not using index)
@ajreal that's only if it's a decimal and not a string. It's possible that the column is a string, but I think it is more likely that it is a decimal so I changed my answer anyway. Can you cite the fact that MySQL ignores indices on numeric columns when compared to a string value?
bugs.mysql.com/bug.php?id=34384 , bugs.mysql.com/bug.php?id=43319 .. for both bug, the example are using really BIG numeric number, however, I believe, the core issue is caused by using the "quote" as it mentioned in the details.

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.