title: json_encode boolean returning wrong value
body: <p>I'm writing a jquery login. If the php login script (checklogin.php) echoes "true" then my jquery page is supposed to give an alert. When I run a login.php (that doesn't use jquery) it sends the request to checklogin.php which echoes "true" however if I use my jquery login script to send the login info to checklogin.php, the alert I trace back says "false".</p>
  
  <pre><code>$('#login').click(function(e){
        $.getJSON("http://www.hedonsoft.com/game/login.php",{username: $("#username").val(), password: $("#password").val()},function(data){
           if(data==true){
              alert(data.boolean);
              //$('#logindiv').slideUp();
              //$('#gamediv').show(); 
              //$('#registerdiv').hide();
              //$('#gameheader').html(data['username']);
           }else{
              alert("false");
           }
        });
  });
  
  &lt;?php
  header('Access-Control-Allow-Origin: *');
  $host="localhost"; // Host name 
  $username=""; // Mysql username 
  $password=""; // Mysql password 
  $db_name="hedonsof_conflict"; // Database name 
  $tbl_name="members"; // Table name 
  
  // Connect to server and select databse.
  mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  mysql_select_db("$db_name")or die("cannot select DB");
  
  // username and password sent from form 
  $myusername=$_POST['myusername']; 
  $mypassword=$_POST['mypassword']; 
  
  // To protect MySQL injection (more detail about MySQL injection)
  $myusername = stripslashes($myusername);
  $mypassword = stripslashes($mypassword);
  $myusername = mysql_real_escape_string($myusername);
  $mypassword = mysql_real_escape_string($mypassword);
  
  $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
  $result=mysql_query($sql);
  
  // Mysql_num_row is counting table row
  $count=mysql_num_rows($result);
  
  // If result matched $myusername and $mypassword, table row must be 1 row
  if($count==1){
  
  // Register $myusername, $mypassword and redirect to file "login_success.php"
  session_register("myusername");
  session_register("mypassword"); 
  $json = array('boolean' =&gt; 'true', 'jsonUser' =&gt; $myusername);
  echo "true";
  }
  else {
  echo "Wrong Username or Password";
  }
  ?&gt;
  </code></pre>
  
  <p>So long story short, when I try to login from a regular php page the value I trace is true, when I try to login from a jquery page the value I trace is false;</p>
  
  <p>EDIT:</p>
  
  <pre><code>&lt;?php
  ...
  $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
  $result=mysql_query($sql);
  
  // Mysql_num_row is counting table row
  $count=mysql_num_rows($result);
  
  // If result matched $myusername and $mypassword, table row must be 1 row
  if($count==1){
  
  // Register $myusername, $mypassword and redirect to file "login_success.php"
  session_register("myusername");
  session_register("mypassword"); 
  $json = array('boolean' =&gt; true, 'jsonUser' =&gt; $myusername);
  echo true;
  }
  else {
  echo "Wrong Username or Password";
  }
  ?&gt;
  </code></pre>
  
  <p>And my jQuery...</p>
  
  <pre><code>$('#login').click(function(e){
        $.getJSON("http://www.hedonsoft.com/game/login.php",{username: $("#username").val(), password: $("#password").val()},function(data){
           if(data){
              alert("true");
              //$('#logindiv').slideUp();
              //$('#gamediv').show(); 
              //$('#registerdiv').hide();
              //$('#gameheader').html(data['username']);
           }else{
              alert(data); // shows null
           }
        });
  });
  </code></pre>
  
  <p>With <code>json_encode</code>, boolean's value is returned <code>null</code></p>
  
  <pre><code>$('#login').click(function(e){
         $.getJSON("http://www.hedonsoft.com/game/login.php",{username: $("#username").val(), password: $("#password").val()},function(data){
            if(data.boolean == "true"){
               alert("true");
               //$('#logindiv').slideUp();
               //$('#gamediv').show(); 
               //$('#registerdiv').hide();
               //$('#gameheader').html(data['username']);
            }else{            alert(data);
            }
         }); });
  </code></pre>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  ...
  // Register $myusername, $mypassword and redirect to file "login_success.php"
  session_register("myusername");
  session_register("mypassword"); 
  $json = array('boolean' =&gt; true, 'jsonUser' =&gt; $myusername);
  echo json_encode($json);
  }
  ...
  ?&gt;
  </code></pre>