简体   繁体   English

PHP脚本检查用户是否已存在于MySQL数据库中

[英]PHP script to check If user already exists in MySQL database

I'm creating simple game for Facebook. 我正在为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']; 所以我有变量$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. 为了在每行我使用echo "number"之后找到不正确的行,所以现在我知道哪一行是不正确的。 It prints 1 2 3 4 5 and stucks. 打印1 2 3 4 5和卡纸。 (everything what are below the code not loading.) So that means this line $results = $stmt->get_result(); (代码下面的所有内容都不会加载。)所以这意味着这行$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. 如果我在这行中注释所有代码加载(然后打印1 2 3 4 5 6 7 No user in database!并且它将post data到用户的墙上。),但是在这种情况下,程序始终会执行相同操作,而不检查数据库。

Also I've tried to change COUNT(*) to COUNT(userName) , but the same. 我也尝试将COUNT(*)更改为COUNT(userName) ,但是相同。

So could you help me, please? 那你能帮我吗?

I've read this: Best way to check for existing user in mySQL database? 我读过这里: 检查mySQL数据库中现有用户的最佳方法? but It not helped me. 但这没有帮助我。

Ps In this case i need to use FB username. 附:在这种情况下,我需要使用FB用户名。

Can you try this, $stmt->fetch() instead of mysqli_fetch_assoc($results) 您可以尝试使用$stmt->fetch()代替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/ 参考: http : //forum.codecall.net/topic/44392-php-5-mysqli-prepared-statements/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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