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

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.

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!

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.