简体   繁体   中英

PHP script to check If user already exists in MySQL database

I'm creating simple game for Facebook. All users who used app are written to database. I need always check If user already exists Is in database, how to do that correctly?

So 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("host","asd","pw","asdf");
                   echo "1";
               $sql = "SELECT COUNT(*) AS num FROM myTable WHERE userName = ?";
                   echo "2";
                if ($stmt = $mysqli->prepare($sql)) {
                   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";
                }
           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";
            }
    }

This code not working, It should post data on user's wall If user not exists in database. I spent many time to find reason why, but unsuccessfully. After debugging It don't show any errors. To find which line is incorrect after every line I used echo "number" so now I know which line is incorrect. It prints 1 2 3 4 5 and stucks. (everything what are below the code not loading.) So that means this line $results = $stmt->get_result(); is incorrect. But I misunderstood what's wrong with this line?

If I comment this line all code loading (then print 1 2 3 4 5 6 7 No user in database! and It post data on user's wall.) but in this case program always do the same, not checking database.

Also I've tried to change COUNT(*) to COUNT(userName) , but the same.

So could you help me, please?

I've read this: Best way to check for existing user in mySQL database? but It not helped me.

Ps In this case i need to use FB username.

Can you try this, $stmt->fetch() instead of mysqli_fetch_assoc($results)

    $mysqli = new mysqli("host","asd","pw","asdf");
echo "1";
/* Create the prepared statement */
$stmt = $mysqli->prepare("SELECT COUNT(*) AS num FROM myTable WHERE userName = ?") or die("Prepared Statement Error: %s\n". $mysqli->error);
    /* Execute the prepared Statement */
    $stmt->execute();

    /* Bind results to variables */
    $stmt->bind_result($name);

    $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();

Ref: http://forum.codecall.net/topic/44392-php-5-mysqli-prepared-statements/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM