将facebook数据存储到mysql

时间:2012-09-29 11:51:40

标签: mysql facebook user-data

  

可能重复:
  Unable to save more than 2 records

我使用Facebook Javascript SDK登录并获取用户信息。当我将这些数据保存到mysql数据库时,它只保存1-2条记录。虽然我每次都会收到用户记录。

if($user){
                print_r($userInfo);
                $userid = $userInfo[id];
                $username = $userInfo[name];
                $birthday = $userInfo[birthday];
                $birthday = date("Y-m-d", strtotime($birthday));
                $userlocation = $userInfo[location][name];
                $gender = $userInfo[gender];
                $useremail = $userInfo[email];
                $sql = "INSERT INTO users (userid, username, useremail, birthday, location, gender) VALUES ('$userid', '$username', '$useremail', '$birthday', '$userlocation', '$gender')";
                if($result = mysql_query($sql)) {
                    echo "success";
                }
            }

用户的表格def是:

1   userid  int(20)         No  None          Change      Drop    Browse distinct values     Primary      Unique      Index  Spatial     Fulltext
 2  username    varchar(30) latin1_swedish_ci       No  None          Change      Drop    Browse distinct values      Primary     Unique      Index  Spatial      Fulltext
 3  useremail   varchar(50) latin1_swedish_ci       No  None          Change      Drop    Browse distinct values      Primary     Unique      Index  Spatial      Fulltext
 4  birthday    date            No  None          Change      Drop    Browse distinct values      Primary     Unique      Index  Spatial     Fulltext
 5  location    varchar(30) latin1_swedish_ci       No  None          Change      Drop    Browse distinct values      Primary     Unique      Index  Spatial      Fulltext
 6  gender  varchar(10) latin1_swedish_ci       No  None          Change      Drop    Browse distinct values      Primary     Unique      Index  Spatial      Fulltext

使用PDO时:

if($user){
                $host = "internal-db.s130813.gridserver.com";
                $db = "db130813_fbmooz";
                $user = "db130813_adminu";
                $pass = "ansari786";
                $conn = new PDO("mysql:host=$host;dbname=$db",$user,$pass);
                $uid = $userInfo[id];
                $uname = $userInfo[name];
                $sql = "INSERT INTO users (userid,username) VALUES (:uid,:uname)";
                $q = $conn->prepare($sql);
                $q->execute(array(':uid'=>$uid, ':uname'=>$uname));

仍然无法保存

1 个答案:

答案 0 :(得分:0)

想到了一些想法:

  • 使用PDO重写此内容 - 不推荐使用'mysql_query'fn
  • 使用预准备语句和'mysql_real_escape_string'来确保值没有任何不良字符
  • 检查返回值并使用'mysql_error()'查看是否出现问题