0

我正在 FB 上收集数据并将其发送到我的数据库。我的应用程序正在运行,我可以看到数据已插入到我的数据库中,但我收到了一个我不知道如何解决的错误。我对此进行了研究并尝试了一些没有结果的解决方案。我添加了一行代码来查看错误是什么。请指导我解决它。谢谢

错误:

 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
 Unknown column 'id' in 'where clause'

错误将我指向这一行:

 $result = mysql_fetch_array($query) or die ( mysql_error () );  

这是我的代码:

<?php
define('db_user','xxx');
define('db_password','xxx');
define('db_host','xxx.xxx.com');
define('db_name','xxx');

// 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() );
mysql_set_charset('utf8',$dbc);

require 'src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId'  => 'xxxx',
'secret' => 'xxxx',
  '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_profile['id']);  
      $result = mysql_fetch_array($query);  


    if(empty($result)){ 
      $query = mysql_query("INSERT INTO users (oauth_provider, oauth_uid, username, first_name, last_name, birthday, email, pic_square ) VALUES    ('facebook', {$user_profile['id']}, '{$user_profile['name']}', '{$user_profile['first_name']}','{$user_profile['last_name']}','{$user_profile['birthday']}','{$user_profile['email']}', '{$user_profile['pic_square']}')");  
  $query = mysql_query("SELECT * FROM users WHERE id = " . mysql_insert_id());  
  $result = mysql_fetch_array($query) or die ( mysql_error () );  


 }  


   // 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);
     }
     ?>
     <html><body>welcome</body></html>
4

2 回答 2

0

我相信失败的查询是这个:

$query = mysql_query("SELECT * FROM users WHERE id = " . mysql_insert_id()); 

如错误消息所述,该表users不包含名为id.

检查数据库中的字段名称,更新查询,它应该可以工作。

于 2012-08-19T15:58:54.250 回答
0

'where 子句'中的未知列'id'

您实际上有一个名为 id 的列吗?检查以确保这确实是您为该列命名的内容。

如果您没有名为 id 的列,则可以使用以下命令添加:

ALTER TABLE `users` ADD `id` INT( 11 ) NOT NULL AUTO_INCREMENT FIRST

使用 AUTO_INCREMENT 列 'id' 将自动为您插入数据库的每一行分配一个新的 id 号,并且您在此处提供的代码将起作用。

于 2012-08-19T16:05:26.493 回答