English 中文(简体)
Facebook PHP SDK——将用户数据输入MYSql数据库
原标题:Facebook PHP SDK - Store user data into MYSql database

我完全是PHP和MySQL的新鲜事,但我是在我的网站上执行Facebook PHP SDK。 至今为止,所有项目都做了罚款,但努力将用户数据输入我的数据库(MySQL)。 我拥有的只是数件,而不是用户名称和 o(我获得两个领域的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);
         }


         ?>

上述网页称为用户网页。 php和用户被转至标识之后的地点。 我试图将“数据库”代码转换为索引.php(用户确实有标识),但迄今所做的一切努力都未成功。 这是指数.php:

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 )
          );
          }

          ?>

<UPDATE* PROBLEM FIXED ($user_profile rather than only$ user) :

  $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);  
    }  
最佳回答

贵国代码中的Face用户数据储存在(用户_profile上,而不是$user下。

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签