Click here to Skip to main content
15,905,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys... i just want to ask why is it that my query is acting weird? you set i just created a query that will search a result based on the id number but when my query generated a result i was surprised because it returned a result... heres my query

SELECT id,name,status from tbl_priviledge where  id >= 'sdfasdfasdf' LIMIT 0, 2


i was expecting to return an empty result but it returned a result... which i know there is no id with a value of
'sdfasdfasdf'
so how do i fix this?
Posted

You should validate your inputs before you use them in queries.

MySQL has some rules for how it compares different types, which explains why 'sdfasdfasdf' evaluates as 0:
http://dev.mysql.com/doc/refman/5.5/en/type-conversion.html[^]
 
Share this answer
 
Comments
vicky patole 21-Feb-12 4:38am    
so graham..you done that
Don't use ">=" - use "=" instead:
SQL
SELECT id,name,status from tbl_priviledge where  id >= 'sdfasdfasdf' LIMIT 0, 2
Becomes
SQL
SELECT id,name,status from tbl_priviledge where  id = 'sdfasdfasdf' LIMIT 0, 2
SQL can compare strings asa well as numbers...
 
Share this answer
 
Comments
Madzmar25 21-Feb-12 3:24am    
actually i need to use >= since i'm generating this code also
SELECT id,name,status from tbl_priviledge where id >= 'sdfasdfasdf' and id < '10' LIMIT 0, 2

That is assuming that the user just type in a bogus input on the first text field and a valid input on the second textfield. i'm providing them with 2 textfield , one is from what id starts and second is to where the id will end
OriginalGriff 21-Feb-12 3:31am    
Then you will have to accept that GIGO still happens! :laugh:
If you want to test for inequility, (i.e. you don't want to search for id = 'sdfasdfasdf') use <> instead of >=. I think the SQL compares the string values using the ascii values of the individual letters. So anything starting with a character bigger than 's' (i.e. t,u,v,...,z) is considered larger than the strings that start with 's'

Or you can use:

SQL
SELECT id,name,status from tbl_priviledge where NOT (id = 'sdfasdfasdf') LIMIT 0, 2
 
Share this answer
 
v2
pay us............;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900