Jump to content

How to check if two alphabetical variables are the same?


Thauwa

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.