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. Quote Link to comment 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()); ?> Quote Link to comment 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()); ?> Quote Link to comment 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. Quote Link to comment 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... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.