Thauwa Posted April 20, 2012 Share Posted April 20, 2012 Hi all! I'm stuck in the midst of some code. In it I have an if function which operates based on whether a variable (a word) is the same as another variable. Here is my code: <? $var1 = "chicago"; if($table_name === $var1){ $result = mysql_query("SELECT * FROM another_table WHERE username='$usernamee'"); while($row = mysql_fetch_array($result) ) { $var2 = $row['var2']; $var2_new = $var2 * 2; mysql_query("UPDATE another_table SET var2 = '$var2_new' WHERE username = '$usernamee'"); } } ?> I tried using =, == and ===, but did not get any of the results I expected. Could anyone advice me on the situation? Thanks in advance. Regards, Thauwa Quote Link to comment https://forums.phpfreaks.com/topic/261295-how-to-check-if-two-alphabetical-variables-are-the-same/ Share on other sites More sharing options...
freelance84 Posted April 20, 2012 Share Posted April 20, 2012 == should work. Have you tried printing the variables in question to check if they are what you expected? Quote Link to comment https://forums.phpfreaks.com/topic/261295-how-to-check-if-two-alphabetical-variables-are-the-same/#findComment-1339006 Share on other sites More sharing options...
l0gic Posted April 20, 2012 Share Posted April 20, 2012 Are you expecting $table_name to be 'chicago' or 'Chicago'? $a == $b Equal TRUE if $a is equal to $b after type juggling. $a === $b Identical TRUE if $a is equal to $b, and they are of the same type. I would try this: <?php $var1 = "chicago"; // Make sure both strings/variables are lower-case. $var1 = strtolower($var1); $table_name = strtolower($table_name); // Echo them out and visually compare them. echo "Does " . $var1 . " == " . $table_name . " ?"; if($table_name == $var1) { $result = mysql_query("SELECT * FROM another_table WHERE username='$usernamee'"); while($row = mysql_fetch_array($result)) { $var2 = $row['var2']; $var2_new = $var2 * 2; mysql_query("UPDATE another_table SET var2 = '$var2_new' WHERE username = '$usernamee'"); } } ?> And visually compare the output. Quote Link to comment https://forums.phpfreaks.com/topic/261295-how-to-check-if-two-alphabetical-variables-are-the-same/#findComment-1339007 Share on other sites More sharing options...
Thauwa Posted April 20, 2012 Author Share Posted April 20, 2012 Hello.. Thanks for your replies. I went through my code and found out that I had put a page refresh line before this code. So I swapped places and the code worked out fine. I really appreciate your time and input. Regards, Thauwa P.S. Hopefully this topic would be of use to a coder out there some day! Quote Link to comment https://forums.phpfreaks.com/topic/261295-how-to-check-if-two-alphabetical-variables-are-the-same/#findComment-1339018 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.