Jump to content

Recommended Posts

Hi could please someone help me out:

i'm coding search fields and need to select in database one table and many columns, but it just wont work, well what i meant you will get when you watch the code:

 

<?
if (
empty($suba)) $suba = ''; 
else $suba = $_REQUEST['suba']; 
if (
empty($subb)) $subb = ''; 
else $subb = $_REQUEST['subb']; 
if (
empty($subc)) $subc = ''; 
else $subc = $_REQUEST['subc']; 
if (
empty($subd)) $subd = ''; 
else $subd = $_REQUEST['subd']; 

$sql = mysql_query("SELECT * FROM r where (suba='$suba' and subb='$subb' and subc='$subc' and subd='$subd')");
   while($r=mysql_fetch_array($sql)) 
   { 
      $title=$r["title"] ;
  	  echo "$title <br>";
} 

?>

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/62459-solved-select-where-many-values/
Share on other sites

It all depends data in the table, are sure that u have data for all search criteria for eg:

SELECT * FROM r where $suba=' ' and $sub='something' and $subc=' ' and $subd='something';

 

if u r using AND operator  we get the result when all the conditions we give are true.

If u r using OR operator we get the result when any of the condition is true.

 

 

yeah but the prob. is that when i select example 3 choices, then it shows all the tree, not those who are exact the 3.

 

Example:

Country

City

Region

 

When i choose just country it should echo all the values, that have exact the same id country as it is selected in search criteria, but if the user chooses country, city and region, it should echo only the values, that have all the three variables, not just one.

Hi

 

A few things:

 

1, what sort of data are you gettting in these variables? You may need to escape them using http://uk2.php.net/mysql_real_escape_string

 

<?php
//......
else $subb = mysql_real_escape_string($_REQUEST['subb']);
//...

 

Also, in you where statement, if the vaue of say $suba is empty ( $suba = ''; ) then the columns in the table must be empty for it to match, i.e it wont skip that... so maybe try:

 

<?php
$where = "";
if (!empty($suba)) 
{ 
  $where .= "suba = '".$_REQUEST['suba']."'"; 
}

// you will have to check if $where is empty, and add the AND for the ones after this.
if (!empty($subb)) 
{ 
  if (strlen($where)>1)
  {
     $where .= " AND ";
  }
  $where .= "suba = '".$_REQUEST['suba']."'"; 
}

$sql = mysql_query("SELECT * FROM r $where");
//...

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.