Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query to only return results when two fields are matched

Status
Not open for further replies.

ncrsjohn

Technical User
Apr 22, 2005
1
US
I am trying to create a query in PHP on a MySql table that only returns records if two out of three fields match.

I only want to return data if the user already knows enough information to guarantee that they get back the record they are entitled to see.

Here is what I have now, which returns a record if I have the correct Last Name (lname), correct address, or company_name. They are like query's so there is a little wiggle room. But I would like to have to get close on both Last Name AND address, or company_name AND address.

// Check for Empty Search Query
if ( $sDisplay == "search" && empty($nNumber) && empty($sLastName) && empty($sCompany) && empty($sAddress) ) {
$aError[] = "<h1>All fields were left empty.</h1>";
$sDisplay = "";
}

// Generate Search Query
if ( $sDisplay == "search" || $sDisplay == "datadump" ) {
$aQuery = array();
$aQuery[] = "order=". $sOrder;
$aQuery[] = "limit=". $nLimit;

if ( $sDisplay == "search" ) {
$SqlSearch = "SELECT mem_num, lname, company_name, address, renew ".
"FROM members ".
"WHERE 1 ";
}
else {
$SqlSearch = "SELECT * FROM members WHERE 1 ";
}

if ( $nIsCurrent == 1 ) {
$aQuery[] = "is_current=1";
$SqlSearch .= " AND renew >= '". date("Y-m-d") ."' ";
}
if ( !empty($nNumber) ) {
$aQuery[] = "mem_num=". $nNumber;
$SqlSearch .= " AND mem_num = '". $nNumber ."' ";
}
if ( !empty($sLastName) ) {
list($sField, $sText) = array("lname", $sLastName);
$SqlSearch .= " AND (". $sField ." LIKE '%". $sText ."%' OR ". $sField ." LIKE '%". $sText ."' OR ". $sField ." LIKE '". $sText ."%') ";
$aQuery[] = $sField ."=". urlencode($sText);
}
if ( !empty($sCompany) ) {
list($sField, $sText) = array("company_name", $sCompany);
$SqlSearch .= " AND (". $sField ." LIKE '%". $sText ."%' OR ". $sField ." LIKE '%". $sText ."' OR ". $sField ." LIKE '". $sText ."%') ";
$aQuery[] = $sField ."=". urlencode($sText);
}
if ( !empty($sAddress) ) {
list($sField, $sText) = array("address", $sAddress);
$SqlSearch .= " AND (". $sField ." LIKE '%". $sText ."%' OR ". $sField ." LIKE '%". $sText ."' OR ". $sField ." LIKE '". $sText ."%') ";
$aQuery[] = $sField ."=". urlencode($sText);
}
}

if ( count($aError) >= 1 ) {
echo "<div class=\"Error\">". implode("<br />", $aError) ."</div>\n";
}
if ( count($aMessage) >= 1 ) {
echo "<div class=\"Message\">". implode("<br />", $aMessage) ."</div>\n";
}


This works as it is.

Thanks for any help anyone can provide.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top