1

How to check if a user exists in the database already?

I have variable $name = $user_profile['name']; It successfully returns user's name.

And this is my part of code to check if user already exists in database.

$user_profile = $facebook->api('/me');
$name = $user_profile['name'];
$mysqli = new mysqli("asd", "asdf", "pw", "asdssst");
/* Create the prepared statement */
$stmt = $mysqli->prepare("SELECT COUNT(*) AS num FROM myTable WHERE userName = ?") or die("Prepared Statement Error: %s\n" . $mysqli->error);
/* Bind results to variables */
$stmt->bind_param('s', $name);
/* Execute the prepared Statement */
$stmt->execute();
$data = $stmt->fetch();
if ($data['num'] > 0) {
    echo "bad";
    print "user already exists\n";
} else {
    echo "good";
    $apiResponse = $facebook->api('/me/feed', 'POST', $post_data);
    print "No user in database\n";
}
/* Close the statement */
$stmt->close();

It always return: good No user in database whether user is in database or not. That means $data['num'] is always 0.

P.S. I know that I need to use FB user ID instead of username, but in this case I need to do that.

1

2 Answers 2

1

I would use something like this

$mysqli = new mysqli('',"","","");
$query = 'SELECT username FROM user WHERE username = ?';
$stmt = $mysqli->prepare($query);
$name = 'asdf';
$stmt->bind_param('s', $name);
$stmt->execute();
$stmt->bind_result($user);
if($stmt->fetch())
   echo 'Username '.$user. ' exists';
else 
  echo 'User doesnt exists';
$stmt->close();
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you for answer, but in both cases I get User doesnt exists nevermind If user is written to database or not.
Maybe this line is incorrect $stmt->bind_result($user); ? Where It get $user value?
bind_result is used to bind data into a variable. for example if your query is requesting two parameters like 'SELECT username, password...' then you tell bind_result($user,$password) to save that data into corresponding variables.
But in code I have other variable $user which contains FB user ID
thats fine. i am using code i have post for my needs and it works, I'm not sure why your doesnt. problem must be somewhere else
|
0

Check if the variable has a result to it. Something like this should work.

//So you have your variable $name here + it returns a value
$name = $user_profile['name'];
//You can put this if statement anywhere below it
if($name > 1){
//log user in
}

5 Comments

Thank you for answer, but I misunderstood where this part of code I need to use?
You need to put this sort of code AFTER you get the results of the query.
Could you update your answer how It looks like in my code, please? Of course If you have time.
Thank you for answer, but what do you mean in this line //log user in what I need to use here? If I use echo $name It successfully returns FB username.
And name is string, how can I check If It is > 0?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.