...

View Full Version : mysql_result



start
09-20-2009, 01:00 AM
Hello,

I have a script that queries the database to see what gender the user is when they are updating their profile information. However, it keeps selecting the male value even after I change the user to female.

PHP code:


$sql_gender_check = mysql_query("SELECT gender FROM users where username='$username'");

$gender_check = mysql_result($sql_gender_check, 0);

if ($gender_check = "Male") {
$gender_type = '<label>
<select name="male" id="gender">
<option value="Undisclosed">Undisclosed</option>
<option value="Male" selected="selected">Male</option>
<option value="Female">Female</option>
</select>
</label>';
} elseif ($gender_check = "Female") {
$gender_type = '<label>
<select name="female" id="gender">
<option value="Undisclosed">Undisclosed</option>
<option value="Male">Male</option>
<option value="Female" selected="selected">Female</option>
</select>
</label>';
}

$smarty->assign('gender_type', $gender_type);


HTML page:


<tr>
<td scope="row">Gender:</td>
<td>
{$gender_type}
</td>
</tr>

MattF
09-20-2009, 01:19 AM
Try:


$sql_gender_check = mysql_query('SELECT gender FROM users where username=\''.$username.'\'') or die(mysql_error());

$gender_check = mysql_result($sql_gender_check, 0);

if ($gender_check = "Male") {
$gender_type = '<label>
<select name="male" id="gender">
<option value="Undisclosed">Undisclosed</option>
<option value="Male" selected="selected">Male</option>
<option value="Female">Female</option>
</select>
</label>';
} elseif ($gender_check = "Female") {
$gender_type = '<label>
<select name="female" id="gender">
<option value="Undisclosed">Undisclosed</option>
<option value="Male">Male</option>
<option value="Female" selected="selected">Female</option>
</select>
</label>';
}

$smarty->assign('gender_type', $gender_type);

start
09-20-2009, 01:36 AM
No success.

PappaJohn
09-20-2009, 01:38 AM
if ($gender_check = "Male")

= is an assignment operator. You want == - a comparison operator

start
09-20-2009, 01:39 AM
Bingo. Thanks.

MattF
09-20-2009, 02:02 AM
Why are the simple things always the easiest to overlook? :D

PappaJohn
09-20-2009, 02:10 AM
Why are the simple things always the easiest to overlook? :D

Just for times like this. :D

@OP: you can avoid these types of problems thus:

if ("Male" == $gender_check)
Specify the value to compare against on the left side of the comparison. Now if you accidentally use = instead of ==, php will throw an error and point you to the problem
Of course, this only works if you're comparing against a static value, as in this case. If you are comparing 2 variables, then an = will still work as an assignment operator



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum