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>";
Talha Tanveer
  • 1,196
  • 8
  • 16
  • If you echo a boolean false it prints nothing so your javascript won't work as is – Adam Apr 15 '14 at 16:27
  • You just can write: `$real='true'; if($swim==0)$real='false';` – Milaci Apr 15 '14 at 16:28
  • 3
    Note, you are using [deprecated](http://www.php.net/manual/en/function.mysql-query.php) functions, consider using `PDO` or `mysqli`. –  Apr 15 '14 at 16:29

2 Answers2

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) . ";";
cmbuckley
  • 40,217
  • 9
  • 77
  • 91
1

Try :

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

Edit:

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

iizno
  • 829
  • 1
  • 9
  • 28