Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: mysql_result

  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    mysql_result

    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:

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

    $gender_check mysql_result($sql_gender_check0);

    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:

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

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Try:

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

    $gender_check mysql_result($sql_gender_check0);

    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); 
    Last edited by MattF; 09-20-2009 at 01:01 AM.

  • #3
    New Coder
    Join Date
    May 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No success.

  • #4
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    PHP Code:
    if ($gender_check "Male"
    = is an assignment operator. You want == - a comparison operator
    John

  • #5
    New Coder
    Join Date
    May 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Bingo. Thanks.

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Why are the simple things always the easiest to overlook?

  • #7
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    Quote Originally Posted by MattF View Post
    Why are the simple things always the easiest to overlook?
    Just for times like this.

    @OP: you can avoid these types of problems thus:
    PHP Code:
    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
    Edit: 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
    Last edited by PappaJohn; 09-20-2009 at 01:13 AM.
    John


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •