title: Facebook PHP SDK- Storing user data in MySql database not working
body: <p>I have been using the FB SDK so that I can get the user to login by their FB account, however during this phase I want their user data stored on my DB and even after following multiple solutions on SO it doesn't seem to work.
  Also the data is printing out from the variable thus proving that there is nothing wrong with that, honestly I think there is something wrong with my sql INSERT statement but I cant seem to see it.</p>
  
  <p>My code for the main page is given below:</p>
  
  <pre><code> &lt;?php
  // Path to PHP-SDK
  include('config.php');
  require '......./src/facebook.php ';
  $facebook = new Facebook(array(
  'appId'  =&gt; '',
  'secret' =&gt; '',
   )); 
  
    /* $facebook = new Facebook(array(
   'appId' =&gt; AppInfo::appID(),
    'secret' =&gt; AppInfo::appSecret(),
    'sharedSession' =&gt; true,
   'trustForwarded' =&gt; true,
     )); */
  
  // See if there is a user from a cookie
  ## connect mysql server
  $mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
  # check connection
  if ($mysqli-&gt;connect_errno) {
      echo "&lt;p&gt;MySQL error no {$mysqli-&gt;connect_errno} : {$mysqli-&gt;connect_error}&lt;/p&gt;";
      exit();}
  $user = $facebook-&gt;getUser();
  
  if ($user) {
   try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook-&gt;api('/me');
    // $fbid = $user_profile['id'];                 // To Get Facebook ID
    $fbuname = $mysqli-&gt;real_escape_string($user_profile['name']);  // To Get Facebook Username
      $fbfname = $mysqli-&gt;real_escape_string($user_profile['first_name']); // To Get Facebook full name
      $fblname = $mysqli-&gt;real_escape_string($user_profile['last_name']); // To Get Facebook full name        
      $fbemail = $mysqli-&gt;real_escape_string($user_profile['email']);    // To Get Facebook email ID
           } catch (FacebookApiException $e) {
           echo '&lt;pre&gt;'.htmlspecialchars(print_r($e, true)).'&lt;/pre&gt;';
       $user = null;
    }
    }
  
   ?&gt;
    &lt;!DOCTYPE html&gt;
  &lt;html xmlns:fb="http://www.facebook.com/2008/fbml"&gt;
  &lt;head&gt;
      &lt;title&gt;Facebook Application Local Development&lt;/title&gt;
      &lt;link type="text/css" rel="stylesheet" href="css/reset.css"&gt;
      &lt;link type="text/css" rel="stylesheet" href="css/main.css"&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="login"&gt;
      &lt;?php if (!$user) { ?&gt;
        &lt;fb:login-button&gt;&lt;/fb:login-button&gt;
      &lt;?php } ?&gt;
  &lt;/div&gt;
  &lt;div id="cont"&gt;
  &lt;!--redirect to local welcome page?!!--&gt;
       &lt;?php if ($user): 
  
  
      // $result = $mysqli-&gt;query("SELECT username from users WHERE username = '$fbuname' LIMIT 1");
    // to check if its getting anything in result
   //print "&lt;pre&gt;";
  //print_r($result);
  //print "&lt;/pre&gt;"; 
  
  
   $result = $mysqli-&gt;query("SELECT username from users WHERE username = '{$fbuname}' LIMIT 1");
   print "&lt;pre&gt;";
   print_r($result);
   print "&lt;/pre&gt;"; 
  if ($result-&gt;num_rows == 0){
  // to check if it enters a loop
  echo "true";
  echo "&lt;br/&gt;" ; 
  $sql= mysqli_query($db, "INSERT INTO users (username,first_name,last_name,email) 
  VALUES ('{$user_profile['name']}', '{$user_profile['first_name']}', '{$user_profile['last_name']}', '{$user_profile['email']}')");
  //$sql= mysqli_query($db,$sql);
  if ( $sql === false ){
   //handled the  SQL error here
   //echo "ERROR";
   die($mysqli-&gt;error);
   }  
  }
  echo "&lt;br/&gt;" ; 
  
  
   echo $fbuname; echo $fbfname; echo $fblname; echo $fbemail; 
       //header("location: welcome.php"); 
      //&lt;!--img src="https://graph.facebook.com/ &lt;//?php echo $user; picture"--&gt;
      //&lt;!--p Hello // echo $user_profile['name']; !&lt;/p--&gt;
  
  
       else: 
      echo "You are not Connected.";
       endif ?&gt;
  </code></pre>