Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7930611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:28:50+00:00 2026-06-03T20:28:50+00:00

I’m totally new to PHP and MySQL but I’m implementing the Facebook PHP SDK

  • 0

I’m totally new to PHP and MySQL but I’m implementing the Facebook PHP SDK in my website. Everything works fine so far but i’m struggling to add the user data to my database (MySQL). Everything I have is a number instead of the username and oauth_uid (I get the number 5 for both fields). This is the code:

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


         ?>

The above page is called user_page.php and it’s where the user is redirected to after the login. I tried to move the “database” code to index.php (where the user does the login) but every effort so far has been unsuccessful. This is the code for index.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 instead of just $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);  
    }  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:28:52+00:00Added an answer on June 3, 2026 at 8:28 pm

    The Facebook user’s data in your code is stored under $user_profile and not $user.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.