marketboy Posted November 10, 2008 Share Posted November 10, 2008 I have a results page that reads a query string from the url, then inserts those variables into the actual query for the results. For example, my string would be: page.php?var1=a&var2=b&var3=c&var4=d$var5=e Then it runs the query: SELECT FROM 'table' WHERE var1 = a AND var2 =b etc... What I need to be able to do is query a range of 2 of the variables. I need to take ($var5 - $var4) as the low number range, and then ($var5 + $var4) as the high number range. For an example, if $var5 = 10, and $var4 = 2, I need to: SELECT FROM 'table' WHERE 'field' BETWEEN (the sum of $var5 and $var4) AND (the difference of $var5 and $var4) in other words, BETWEEN 12-8. Any assistance would be much appreciated! Thank you in advance. Link to comment https://forums.phpfreaks.com/topic/132190-query-a-range-between-variables/ Share on other sites More sharing options...
rhodesa Posted November 10, 2008 Share Posted November 10, 2008 <?php $sum = $_GET['var5'] + $_GET['var4']; $diff = $_GET['var5'] - $_GET['var4']; $sql = sprintf('SELECT * FROM `table` WHERE `field` BETWEEN %d AND %d',$diff,$sum); $result = mysql_query($sql) or die(mysql_error()); ?> Link to comment https://forums.phpfreaks.com/topic/132190-query-a-range-between-variables/#findComment-687038 Share on other sites More sharing options...
marketboy Posted November 10, 2008 Author Share Posted November 10, 2008 Here is what I currently have, and the query worked until I adjusted to your suggestions. Not sure if I adjusted something incorrectly? I am receiving the following: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource... Thanks again! <?php $var1 = @$_GET['var1'] ; $var2 = @$_GET['var2'] ; $var3 = @$_GET['var3'] ; $var4 = @$_GET['var4'] ; $var5 = @$_GET['var5'] ; $sum = $_GET['var5'] + $_GET['var4']; $diff = $_GET['var5'] - $_GET['va4']; $query = "SELECT var1, var2, var3 FROM Table WHERE var5 BETWEEN %d AND %d,$diff,$sum order by var1"; $result = mysql_query($sql) or die(mysql_error()); ?> Link to comment https://forums.phpfreaks.com/topic/132190-query-a-range-between-variables/#findComment-687071 Share on other sites More sharing options...
fenway Posted November 10, 2008 Share Posted November 10, 2008 Seriously? You just threw away the sprintf() function. Link to comment https://forums.phpfreaks.com/topic/132190-query-a-range-between-variables/#findComment-687136 Share on other sites More sharing options...
rhodesa Posted November 10, 2008 Share Posted November 10, 2008 yeah...can't just drop the sprintf func...and, you store the string into $query, and execute $sql... Link to comment https://forums.phpfreaks.com/topic/132190-query-a-range-between-variables/#findComment-687215 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.