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....
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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>

 

??? 

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.