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

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.