我有一个函数可以检查数据库中是否存在用户数据并返回用户的电子邮件ID。如果它没有退出,那么它会插入并应该返回插入的用户电子邮件。
我的部分功能
function checkUser($userdata){
$oauth_uid = $userdata->id;
$email = $userdata->emailAddress;
$check = $this->db->query("SELECT * FROM $this->userTable WHERE oauth_uid = '".$oauth_uid."' AND email = '".$email."'");
if(mysqli_num_rows($check) > 0){
$result = $check->fetch_array(MYSQLI_ASSOC);
$query = "UPDATE $this->userTable SET fname = '".$userdata->firstName."', lname = '".$userdata->lastName."', email = '".$userdata->emailAddress."', location = '".$userdata->location->name."', country = '".$userdata->location->country->code."', picture_url = '".$userdata->pictureUrl."', profile_url = '".$userdata->publicProfileUrl."', modified = '".date("Y-m-d H:i:s")."' WHERE id = ".$result['id'];
$this->db->query($query);
return $result['id']; //this works it returns email of user if exists
}else{
$query = "INSERT INTO
$this->userTable(oauth_provider,oauth_uid,fname,lname,email,location,country,picture_url,profile_url,created,modified)
VALUES('linkedin','".$userdata->id."','".$userdata->firstName."','".$userdata->lastName."','".$userdata->emailAddress."','".$userdata->location->name."','".$userdata->location->country->code."','".$userdata->pictureUrl."','".$userdata->publicProfileUrl."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
$this->db->query($query);
$id = $this->db->insert_id;
$check = $this->db->query("SELECT * FROM $this->userTable WHERE oauth_uid = '".$id."'");
if(mysqli_num_rows($check) > 0){
$result = $check->fetch_array(MYSQLI_ASSOC);
return $result['id']; //this doesnt work. It inserts the data into database but doesn't return anything.
}
}
}
if语句正常工作。它检查用户是否存在,然后返回电子邮件。
其他部分有效。它将用户插入数据库但不返回电子邮件 我怎样才能做到这一点。
感谢。
答案 0 :(得分:1)
您的代码表示您要返回用户ID,而不是用户电子邮件,如果是这样 使用
return $check->insert_id;
而不是if(mysqli_num_rows($ check)> 0)块
但如果是您打算获得的用户电子邮件,那么只需从userdata对象返回
return $userdata->emailAddress;
希望有所帮助