I'm creating app for Facebook, all users who used app are written to database. So I need always check If user already Is in database, how to do that correctly?
So I have variable $name = $user_profile['name']; It get user's name
This is my function to insert user to database.
$mysqli = new mysqli("asd","asd","pw","asd");
if ($stmt = $mysqli->prepare("INSERT into myTable (score, userName) VALUE (?,?) ")) {
if (!$mysqli->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
}
$stmt->bind_param('ss', $score, $name);
$stmt->execute();
if ($stmt->error != '') {
echo ' error:'.$stmt->error;
} else {
echo 'success';
}
$stmt->close();
} else {
echo 'error:'.$mysqli->error;
}
$mysqli->close();
Now I need If function to check If user already in database. As I understand It should be similiar as insert to database, just instead INSERT into I need to use SELECT from just how to do It successfully? Thank you.
I've read this: Best way to check for existing user in mySQL database? but It wont helped me.
UPDATE
Now my code looks like:
$mysqli = new mysqli("host","asd","pw","asdf");
echo "1";
$sql = "SELECT COUNT(*) AS num FROM myTable WHERE userName = ?";
echo "2";
if ($stmt = $mysqli->prepare("SELECT COUNT(*) AS num FROM under WHERE userName = ? ")) {
echo "3";
$stmt->bind_param('s', $name);
echo "4";
$stmt->execute();
echo "5";
$results = $stmt->get_result();
echo "6";
$data = mysqli_fetch_assoc($results);
echo "7";
}
It print 1 2 3 4 5. that means this line $results = $stmt->get_result(); is incorrect, because after It not printing value. What can be problem here?