Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Facebook PHP SDK-将用户数据存储到MYSql数据库中_Php_Mysql_Facebook - Fatal编程技术网

Facebook PHP SDK-将用户数据存储到MYSql数据库中

Facebook PHP SDK-将用户数据存储到MYSql数据库中,php,mysql,facebook,Php,Mysql,Facebook,我对PHP和MySQL完全陌生,但我正在我的网站上实现Facebook PHP SDK。到目前为止一切正常,但我正在努力将用户数据添加到我的数据库(MySQL)中。我所有的东西都是一个数字,而不是用户名和oauth_uid(两个字段的数字都是5)。代码如下: <?php define('db_user','myUserName'); define('db_password','myPassword'); define('db_host','myHost'); define

我对PHP和MySQL完全陌生,但我正在我的网站上实现Facebook PHP SDK。到目前为止一切正常,但我正在努力将用户数据添加到我的数据库(MySQL)中。我所有的东西都是一个数字,而不是用户名和oauth_uid(两个字段的数字都是5)。代码如下:

 <?php
  define('db_user','myUserName');
  define('db_password','myPassword');
  define('db_host','myHost');
  define('db_name','myDbName');

  // Connect to MySQL
  $dbc = @mysql_connect (db_host, db_user, db_password) OR die ('Could not connect to  MySQL: ' . mysql_error() );
    // Select the correct database
   mysql_select_db (db_name) OR die ('Could not select the database: ' . mysql_error() );

    require 'lib/facebook.php';

  // Create our Application instance (replace this with your appId and secret).
  $facebook = new Facebook(array(
  'appId'  => 'MYAPPID',
  'secret' => 'MYSECRET',
      'cookie' => true));


    // Get User ID
     $user = $facebook->getUser();

    // We may or may not have this data based on whether the user is logged in.
   //
  // If we have a $user id here, it means we know the user is logged into
 // Facebook, but we don't know if the access token is valid. An access
 // token is invalid if the user logged out of Facebook.

   if ($user) {
    try {
    // Proceed knowing you have a logged in user who's authenticated.
    $fbuid = $facebook->getUser();
     $user_profile = $facebook->api('/me');



       } catch (FacebookApiException $e) {
       error_log($e);
       $user = null;
       }
         }else{
       header('Location: index.php');

        }
          $query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'facebook' AND     oauth_uid = ". $user['id']);  
          $result = mysql_fetch_array($query);  

        if(empty($result)){ 
          $query = mysql_query("INSERT INTO users (oauth_provider, oauth_uid, username)         VALUES ('facebook', {$user['id']}, '{$user['name']}')");  
           $query = msyql_query("SELECT * FROM users WHERE id = " . mysql_insert_id());  
          $result = mysql_fetch_array($query);  
           }  



       // Login or logout url will be needed depending on current user state.
        if ($user) {
         $paramsout = array('next'=>'http://www.mywebsite.com/test/logout.php');
         $logoutUrl = $facebook->getLogoutUrl($paramsout);
         }


         ?>
更新*问题已修复($user\u profile而不是$user):


您代码中Facebook用户的数据存储在
$user\u profile
下,而不是
$user

require 'lib/facebook.php';

// Create our Application instance (replace this with your appId and secret).
  $facebook = new Facebook(array(
   'appId'  => 'MYAPPID',
   'secret' => 'MYSECRET',
       'cookie' => true));


    // Get User ID
    $user = $facebook->getUser();


    if ($user) {
    try {
      // Proceed knowing you have a logged in user who's authenticated.
      $fbuid = $facebook->getUser();
      $user_profile = $facebook->api('/me');

      header('Location: user_page.php');

      } catch (FacebookApiException $e) {
      error_log($e);
     $user = null;
    }
     }


     // Login or logout url will be needed depending on current user state.
     if ($user) {
      $logoutUrl = $facebook->getLogoutUrl();

     } else {

        $loginUrl = $facebook->getLoginUrl(Array('scope'=>    'user_interests,user_activities,user_education_history,user_likes,user_about_me,   user_birthday, user_groups, user_hometown, user_work_history, email',
          'redirect_uri' => 'http://www.mywebpage.com/test/user_page.php')
          );
          }

          ?>
  $query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'facebook' AND  oauth_uid = ". $user_profile['id']);  
   $result = mysql_fetch_array($query);  

  if(empty($result)){ 
  $query = mysql_query("INSERT INTO users (oauth_provider, oauth_uid, username) VALUES    ('facebook', {$user_profile['id']}, '{$user_profile['name']}')");  
  $query = msyql_query("SELECT * FROM users WHERE id = " . mysql_insert_id());  
  $result = mysql_fetch_array($query);  
    }