MySQL Forums
Forum List  »  PHP

SELECT and WHERE Statements
Posted by: mr.andersen
Date: April 07, 2006 09:22PM

Here's my code:

<html>
<head>
<title>Search</title>
</head>

<?
include("connect/connectne.php");

$NAME = $_POST['sendName'];
$CITY = $_POST['sendCity'];
$STATE = $_POST['sendState'];
$ZIPCODE = $_POST['sendZipcode'];

$query = "SELECT * FROM CCC WHERE State = '$STATE' OR City = '$CITY'";

$result = mysql_query($query);

while($row = mysql_fetch_array($result))
{
extract($row);
echo "$LicenseNumber &nbsp - &nbsp $LicenseeName &nbsp - &nbsp $City &nbsp - &nbsp $State<br>";
}
?>

My problem is with line 14 or this line:

$query = "SELECT * FROM CCC WHERE State = '$STATE' OR City = '$CITY'";

Specifically my problem is with OR. If I put in OR and someone were to search both City and State, say like Omaha NE it would return all of the results for Nebraska regardless of the city of Omaha. If I were to change the OR to AND and someone put nothing in the City name but put in the state of NE the result would return zero because the query did not fulfill BOTH parts of the AND expression.

The question:

How do I make it so that if someone puts in both Omaha NE it will search both city and state but if they leave out the city or state it will not return zero results? Make sense? Any help appreciated. Thanks.

CJ

Options: ReplyQuote


Subject
Written By
Posted
SELECT and WHERE Statements
April 07, 2006 09:22PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.