0

I am having some problems with setting the value of "swimsuit" variable as defined below, however when I set the value to "1" in my MYSQL database, it sets $real=true but elsewise it says "real is undefined".

$query4 = "SELECT swimsuit FROM player_equipment WHERE player_name='xhyderz'";
$result = mysql_query($query4,$link);
$row = mysql_fetch_assoc($result);
$swim = $row['swimsuit'];
$real=null;
if($swim==0){
    $real=false;
}else{
    $real=true;
}
echo "<script type='text/javascript'> var swimsuit=".$real."; </script>";
3
  • If you echo a boolean false it prints nothing so your javascript won't work as is Commented Apr 15, 2014 at 16:27
  • You just can write: $real='true'; if($swim==0)$real='false'; Commented Apr 15, 2014 at 16:28
  • 3
    Note, you are using deprecated functions, consider using PDO or mysqli. Commented Apr 15, 2014 at 16:29

2 Answers 2

4

When you concatenate a boolean variable with a string like that, you get the following:

var_dump(true  . ""); // "1"
var_dump(false . ""); // ""

So if $real is false, your JavaScript looks like:

var swimsuit=;

You can either use the strings "true" or "false" for $real, or you can use json_encode:

echo "var swimsuit=" . json_encode($real) . ";";
Sign up to request clarification or add additional context in comments.

Comments

1

Try :

if($swim==0){
    $real="false";
}else{
    $real="true";
}

Edit:

Look at cbuckley's explaination, way more clear and complete than mine.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.