I have a function that checks if user data exists in database and returns email id of user. If it doesnt exits then it inserts and should return the inserted user email.
Part of my function
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.
}
}
}
The if statement works properly. It checks if user exists then returns the email.
The else part works partially. It inserts the user into database but does not return the email how can i make this work.
Thanks.