title: mysqli bind_param not setting string to null
body: <p>I'm trying to use the mysqli <code>bind_param</code> function in my website for the process of registering users.  I want to do two things:  </p>
  
  <ol>
  <li><p>If the user registers with the registration form I provide, set the users password in the database to their desired password (hashed and salted, of course); </p></li>
  <li><p>if they use Facebook registration, set the password field to NULL.</p></li>
  </ol>
  
  <p>I realize that I can set the default of the password column in the database to NULL and simply not specify it in the statement upon registering, but this problem also carries on to other aspects of the site where this would not be a practical resolution.</p>
  
  <pre><code>$password = null;
  
  $stmt-&gt;bind_param('ssssss', $email, $password, $fullname,
           $prof_image_url, $splash_image_url, $facebook_id);
  </code></pre>
  
  <p>For testing purposes, I set the variable <code>$password</code> to null directly before the statement is called and it still occurs.  When the query is executed, it results in a row with the password column empty rather than saying NULL when I view it via command line.</p>
  
  <p>All help is greatly appreciated!</p>
  
  <p>EDIT:
  Here's the structure of the password column...
  Field: password,
  Type: varchar(255),
  Null: YES,
  Key:,
  Default: NULL,
  Extra:</p>
  
  <pre><code>function user_add($mysqli, $email, $password, $fullname, $prof_image_url = null, $splash_image_url = null, $facebook_id = null) {
      if ($mysqli == null) throw new Exception('Could not connect to database.');
      if ($email == null || $email == '') throw new Exception('Email is required.', 1);
      if ($password != null &amp;&amp; $password == '') throw new Exception('Password is required.', 1);
      if ($fullname == null || $fullname == '') throw new Exception('Name is required.', 1);
      if ($prof_image_url == null) $prof_image_url = IMAGE_PATH . '/profile_default.png';
      if ($splash_image_url == null) $splash_image_url = IMAGE_PATH . '/splash_bg_1.jpg';
  
      $email = $mysqli-&gt;real_escape_string($email);
      $password = $mysqli-&gt;real_escape_string($password);
      $fullname = $mysqli-&gt;real_escape_string($fullname);
      $facebook_id = $mysqli-&gt;real_escape_string($facebook_id);
  
      $stmt = $mysqli-&gt;prepare('INSERT INTO users(`email`, `password`, `full_name`, `prof_image_url`, `splash_image_url`, `facebook_id`) VALUES(?, ?, ?, ?, ?, ?);');
  
      if (!$stmt) {
          throw new Exception('Could not connect to database.');
      }
  
      $stmt-&gt;bind_param('ssssss', $email, $password, $fullname, $prof_image_url, $splash_image_url, $facebook_id);
  
      if (!$stmt-&gt;execute()) {
          throw new Exception('Could not register user.', 1);
      }
  
      return $mysqli-&gt;insert_id;
  }
  </code></pre>
  
  <p>And this is where I call the function:</p>
  
  <pre><code>function account_facebook_register($mysqli, $code){
      $token_url = "https://graph.facebook.com/oauth/access_token?"
      . "client_id=" . FACEBOOK_APP_ID
      . "&amp;redirect_uri=" . urlencode(FACEBOOK_REDIRECT_URL)
      . "&amp;client_secret=" . FACEBOOK_APP_SECRET
      . "&amp;code=" . $code
      . "&amp;scope=email";
  
      $response = @file_get_contents($token_url);
  
      $params = null;
      parse_str($response, $params);
      $token = $params['access_token'];
  
      $graph_url = "https://graph.facebook.com/me?access_token=" . $token;
  
      $user = json_decode(file_get_contents($graph_url));
  
      if (!isset($user-&gt;email)){
          $_SESSION['error'] = 'Invalid email on Facebook account.';
          $_SESSION['error_level'] = 1;
          header('Location: ' . WEB_ROOT . '/account/register.php');
          die;
      }
  
      $user_exists = user_getByEmail($mysqli, $user-&gt;email);
  
      if ($user_exists == null){
          $mysqli = mysql_create();
          $password = null;
          $uid = user_add($mysqli, $user-&gt;email, $password, $user-&gt;name, 'https://graph.facebook.com/' . $user-&gt;id . '/picture?type=large', null, $user-&gt;id);
  
          $token = facebook_get_long_token($token);
          token_add($mysqli, $uid, 'facebook', $token, $user-&gt;id);
  
          $_SESSION['fb_token'] = $token;
          $_SESSION['uid'] = $uid;
          $_SESSION['success'] = "Registration Successful!";
      }else{
          account_facebook_login($mysqli, $user_exists-&gt;uid);
      }
  }
  </code></pre>
  
  <p>The mysql_create function just sets up the database for accessing and works as intended.
  Thank you!</p>
  
  <p>Answer:
  mysqli->real_escale_string makes null values empty.  Check if variable is null before you escape it.</p>