0

I have a Db query which should return value:

$gbe=mysql_query("SELECT userid FROM `gift_stage` WHERE amount='500' AND (`assign` > '1' AND `assign` < '18') AND userid !='".$class_session->isuser()."' ORDER BY id ASC LIMIT 1");

but it is not showing any value, but when I replace AND (assign> '1' ANDassign< '18') with AND (assign> '1') it is giving the expected result. But I am not getting that where is the problem, because the field assign alread have the value '13' in database

please help me guys

all fields are varchar

Haren Sarma
  • 173
  • 1
  • 1
  • 9
  • 3
    Do not use varchars instead of numbers when doing numeric comparisons. `select '14' > '132' from dual;` will return true. – Sebas Sep 23 '14 at 19:43
  • are you sure amount column has value 500 where assign value is 13 – radar Sep 23 '14 at 19:43
  • 2
    `mysql_query` is an obsolete interface and should not be used in new applications and will be removed in future versions of PHP. A modern replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/). If you're new to PHP, a guide like [PHP The Right Way](http://www.phptherightway.com/) can help explain best practices. – tadman Sep 23 '14 at 19:50

2 Answers2

2

Don't put quotes on your numbers otherwise they will be treated as varchars.

2

Remove the parenthesis and compare numbers instead of text strings

SELECT userid 
FROM `gift_stage` 
WHERE amount='500' 
AND `assign` > 1 
AND `assign` < 18 
AND userid !='".$class_session->isuser()."' 
ORDER BY id ASC LIMIT 1

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi. This article will help you decide which.

Community
  • 1
  • 1
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119