Jump to content

SQL with more than one WHERE condition


imimin

Recommended Posts

Hey imimin,

 

When passing special characters through the url, they will be converted to the html version of that character.  However, when you parse it back in php they will be separated by the delimiter you specified.  So the following should do the trick.

 

<?php
if(isset($_GET['cat'])){
$cats = explode(",",$_GET['cat']);
$qs = "";
foreach($cats as $cat){
	$cat = mysql_real_escape_string($cat);
	$qs .= "'$cat',";
}
$qs = substr($qs,0,-1);
$query = "SELECT prod_name FROM products WHERE cat IN($qs);";
$run = mysql_query($query);
// .... parse query....
}
?>

Now, is there a way to set this up in a link?  Such as :

 

<A HREF="<http://www.anysite.com  . "?" . "cat=" . "red, blue,yellow">" TITLE="">TEST PAGE</A>

 

Sure, try the link above and use explode with "," as the string to separate the cat variables, (or ", " if there is going to be a space after the commas).  Perhaps the following code might work?

 

<?php
$catArray = explode(",", $_GET['cat']);
$query = "SELECT prod_name FROM products WHERE cat IN('$catArray[0]','$catArray[1]','$catArray[2]');"
?>

 

EDIT: Nevermind, I forgot about the special character thing posted above.

Now, is there a way to set this up in a link?  Such as :

 

<A HREF="<http://www.anysite.com  . "?" . "cat=" . "red, blue,yellow">" TITLE="">TEST PAGE</A>

 

Sure, try the link above and use explode with "," as the string to separate the cat variables, (or ", " if there is going to be a space after the commas).  Perhaps the following code might work?

 

<?php
$catArray = explode(",", $_GET['cat']);
$query = "SELECT prod_name FROM products WHERE cat IN('$catArray[0]','$catArray[1]','$catArray[2]');"
?>

 

EDIT: Nevermind, I forgot about the special character thing posted above.

 

Another reason I wouldn't advise using this model is because you'd be restricted to only 3 categories, that's why you generate the IN portion of the query automatically.

<?php

if(isset($_GET['cat'])){

  $cats = explode(",",$_GET['cat']);

  $qs = "";

  foreach($cats as $cat){

      $cat = mysql_real_escape_string($cat);

      $qs .= "'$cat',";

  }

  $qs = substr($qs,0,-1);

  $query = "SELECT prod_name FROM products WHERE cat IN($qs);";

  $run = mysql_query($query);

  // .... parse query....

}

?>

 

p2grace, are you saying I can use the above code to "receive" the data sent from the link in the format of:

 

<A HREF="<http://www.anysite.com  . "?" . "cat=" . "red, blue,yellow">" TITLE="">TEST PAGE</A>

 

??? 

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.