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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Related: [How to check if a row exists in MySQL?](https://stackoverflow.com/a/58942841/1839439) – Dharman Nov 27 '19 at 21:32

2 Answers2

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();
Marko
  • 1,337
  • 5
  • 18
  • 37
  • Thank you for answer, but in both cases I get `User doesnt exists` nevermind If user is written to database or not. – user3097011 Dec 12 '13 at 21:02
  • Maybe this line is incorrect `$stmt->bind_result($user);` ? Where It get $user value? – user3097011 Dec 12 '13 at 21:05
  • 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. – Marko Dec 12 '13 at 21:11
  • But in code I have other variable $user which contains FB user ID – user3097011 Dec 12 '13 at 21:16
  • 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 – Marko Dec 12 '13 at 21:41
  • This code is incorrect as I see, after If is missing `{` before and after else missing `{}` too – user3097011 Dec 13 '13 at 09:06
  • you dont need brackets if there is only one statement after – Marko Dec 13 '13 at 10:22
  • I found problem, but misunderstood why It happens. If I use `$name = 'FName LName'` It works. But If I get facebook name `$user_profile = $facebook->api('/me'); $name = $user_profile['name'];` It not working. But If I use `echo $name` It successfully print It. – user3097011 Dec 13 '13 at 13:15
  • And I found 1 more thing. It not working because here is no UTF-8 encoding. How to add It? – user3097011 Dec 13 '13 at 13:22
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
}
ChaseC
  • 426
  • 2
  • 6
  • 18