Jump to content

Query a range between variables?


marketboy

Recommended Posts

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

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());

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.