Jump to content

[SOLVED] Why is my IF statement not working?


iBlizz

Recommended Posts

Code is below...2 if statements.  Based on what the $searchType variable is.  However, it doesn't matter, both blocks of code execute.

 

$searchType echos "state" however the block in if $searchType="country" executes as well.  Vice versa when searchType is country, state also executes.

 

I'm just learning PHP, but I'm pretty sureI have if statements down...I'm puzzled at this though.

 

echo $searchType;

if ($searchType = "state") {
echo "Searching by state";
mysql_connect($host, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

$ambassadors = mysql_query("SELECT * FROM ambassadors WHERE state='$searchValue'") or die(mysql_error());  

while ($ambassador = mysql_fetch_array($ambassadors)) {
	echo "<tr><td>"; 
	echo "<iframe src=\"http://gamercard.xbox.com/" . $ambassador['gamertag'] . ".card\" scrolling=\"no\" frameBorder=\"0\" height=\"140\" width=\"204\">" . $ambassador['gamertag'] . "</iframe>";
	echo "</td><td>"; 

	$stateSearch = mysql_query("SELECT state FROM state WHERE id='$searchValue'") or die(mysql_error());

	while ($state = mysql_fetch_array($stateSearch)) {
	echo $state['state'];
	}

	echo "</td></tr>"; 
}
}

if ($searchType = "country") {
echo "Searching by country";
mysql_connect($host, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

$ambassadors = mysql_query("SELECT * FROM ambassadors WHERE country='$searchValue'") or die(mysql_error());  

while ($ambassador = mysql_fetch_array($ambassadors)) {
	echo "<tr><td>"; 
	echo "<iframe src=\"http://gamercard.xbox.com/" . $ambassador['gamertag'] . ".card\" scrolling=\"no\" frameBorder=\"0\" height=\"140\" width=\"204\">" . $ambassador['gamertag'] . "</iframe>";
	echo "</td><td>"; 

	$countrySearch = mysql_query("SELECT country FROM country WHERE id='$searchValue'") or die(mysql_error());

	while ($country = mysql_fetch_array($countrySearch)) {
	echo $country['country'];
	}

	echo "</td></tr>"; 
}
}
?>

because you are assigning "state" to $searchType

 

if ($searchType = "state") {

if ($searchType = "country") {

 

You will want to do a comparison, like this (two equal signs):

 

if ($searchType == "state") {

if ($searchType == "country") {

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.