Reputation:
My SQL task $query in PHP isn't working with values that has different amount of numbers (example. XXX and XXXX => 700 and 1100,800 and 1200 and 900 and 1300:
//Applying values from <form>:
$pirmas = $_POST['ieskoti'] - 200;
$antras = $_POST['ieskoti'] + 200;
// So when $_POST['ieskoti'] is 800, 900, 1000 and 1100, this task is not working:
$query = 'SELECT * FROM prekes WHERE prek_kaina BETWEEN "'. $pirmas .'" AND "'. $antras .'";';
$result = mysql_query($query);
With other values it works perfectly, because BETWEEN operator works with values with same amount of numbers: 500 and 900, 1000 and 1400 and etc...
Any ideas?
Upvotes: 0
Views: 1340
Reputation: 121
Please Try this,
//Applying values from <form>:
$pirmas = $_POST['ieskoti'] - 200;
$antras = $_POST['ieskoti'] + 200;
// So when $_POST['ieskoti'] is 800, 900, 1000 and 1100, this task is not working:
$query = 'SELECT * FROM `prekes`
WHERE prek_kaina BETWEEN "'. $pirmas .'" AND "'. $antras .'"';
$result = mysql_query($query);
Upvotes: 0
Reputation:
You're enclosing your range values in quotes, so MySQl is doing a string comparison. Remove the quotes for numeric values. Try this:
$query = 'SELECT * FROM prekes WHERE prek_kaina BETWEEN '. $pirmas .' AND '. $antras .';';
// Double quotes removed ^ ^ ^ ^
Upvotes: 4
Reputation: 4383
Try removing the quotes around your numbers, so they are not interpreted as strings:
$query = 'SELECT * FROM prekes WHERE prek_kaina BETWEEN '. $pirmas .' AND '. $antras .';';
Upvotes: 4