Php mysqli bind_参数未将字符串设置为null

Php mysqli bind_参数未将字符串设置为null,php,mysql,sql,parameters,mysqli,Php,Mysql,Sql,Parameters,Mysqli,我试图在我的网站中使用mysqlibind_param功能来注册用户。我想做两件事: 如果用户使用我提供的注册表进行注册,则将数据库中的用户密码设置为他们所需的密码(当然是散列和盐渍) 如果他们使用Facebook注册,请将密码字段设置为NULL 我意识到,我可以将数据库中密码列的默认值设置为NULL,并且在注册时不在声明中指定它,但是这个问题也会影响到网站的其他方面,这不是一个实际的解决方案 $password = null; $stmt->bind_param('ssssss', $

我试图在我的网站中使用mysqli
bind_param
功能来注册用户。我想做两件事:

  • 如果用户使用我提供的注册表进行注册,则将数据库中的用户密码设置为他们所需的密码(当然是散列和盐渍)

  • 如果他们使用Facebook注册,请将密码字段设置为NULL

  • 我意识到,我可以将数据库中密码列的默认值设置为NULL,并且在注册时不在声明中指定它,但是这个问题也会影响到网站的其他方面,这不是一个实际的解决方案

    $password = null;
    
    $stmt->bind_param('ssssss', $email, $password, $fullname,
             $prof_image_url, $splash_image_url, $facebook_id);
    
    出于测试目的,在调用语句之前,我直接将变量
    $password
    设置为null,并且该变量仍然存在。当执行查询时,它会生成一个密码列为空的行,而不是在通过命令行查看时说NULL

    非常感谢您的帮助

    编辑: 下面是密码列的结构。。。 字段:密码, 类型:varchar(255), 空:是的, 关键字:, 默认值:NULL, 额外:

    这就是我调用函数的地方:

    function account_facebook_register($mysqli, $code){
        $token_url = "https://graph.facebook.com/oauth/access_token?"
        . "client_id=" . FACEBOOK_APP_ID
        . "&redirect_uri=" . urlencode(FACEBOOK_REDIRECT_URL)
        . "&client_secret=" . FACEBOOK_APP_SECRET
        . "&code=" . $code
        . "&scope=email";
    
        $response = @file_get_contents($token_url);
    
        $params = null;
        parse_str($response, $params);
        $token = $params['access_token'];
    
        $graph_url = "https://graph.facebook.com/me?access_token=" . $token;
    
        $user = json_decode(file_get_contents($graph_url));
    
        if (!isset($user->email)){
            $_SESSION['error'] = 'Invalid email on Facebook account.';
            $_SESSION['error_level'] = 1;
            header('Location: ' . WEB_ROOT . '/account/register.php');
            die;
        }
    
        $user_exists = user_getByEmail($mysqli, $user->email);
    
        if ($user_exists == null){
            $mysqli = mysql_create();
            $password = null;
            $uid = user_add($mysqli, $user->email, $password, $user->name, 'https://graph.facebook.com/' . $user->id . '/picture?type=large', null, $user->id);
    
            $token = facebook_get_long_token($token);
            token_add($mysqli, $uid, 'facebook', $token, $user->id);
    
            $_SESSION['fb_token'] = $token;
            $_SESSION['uid'] = $uid;
            $_SESSION['success'] = "Registration Successful!";
        }else{
            account_facebook_login($mysqli, $user_exists->uid);
        }
    }
    
    mysql_create函数只是设置用于访问的数据库,并按预期工作。 谢谢大家!

    答复: mysqli->real\u escale\u字符串使空值为空。在转义变量之前,请检查变量是否为空。

    经过进一步研究后,这两个变量都表明您的问题应该按照目前的方式解决,因此,关于导致意外行为的代码,您的问题中还没有其他内容

    你的更新给出了答案。当您
    real\u escape\u string
    空值时,会导致结果为空

    以测试数据库结构为例:

    CREATE TABLE `example` (
      `example_id` mediumint(9) NOT NULL AUTO_INCREMENT,
      `non_null_val` char(64) NOT NULL,
      `null_val` char(64) DEFAULT NULL,
      PRIMARY KEY (`example_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
    
    然后,运行以下PHP代码:

    <?php
      $mysqli = new mysqli('localhost', 'demo', 'exampledemo', 'demo');
      if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
      }
    
      $stmt = $mysqli->prepare("INSERT INTO example (non_null_val, null_val) VALUES (?, ?)");
      $non_null_val = 'ABC';
      $null_val = null;
      $stmt->bind_param('ss', $non_null_val, $null_val);
      $stmt->execute();
    
      $non_null_val = 'DEF';
      $null_val = null;
      $stmt->bind_param('ss', $non_null_val, $mysqli->real_escape_string($null_val));
      $stmt->execute();
    
      $stmt->close();
    
      $mysqli->close();
    ?>
    
    提供您所需的信息。

    经过进一步研究后,两者都表明您的问题应按目前的方式解决,因此,您的问题中还存在一些关于导致意外行为的代码的问题

    你的更新给出了答案。当您
    real\u escape\u string
    空值时,会导致结果为空

    以测试数据库结构为例:

    CREATE TABLE `example` (
      `example_id` mediumint(9) NOT NULL AUTO_INCREMENT,
      `non_null_val` char(64) NOT NULL,
      `null_val` char(64) DEFAULT NULL,
      PRIMARY KEY (`example_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
    
    然后,运行以下PHP代码:

    <?php
      $mysqli = new mysqli('localhost', 'demo', 'exampledemo', 'demo');
      if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
      }
    
      $stmt = $mysqli->prepare("INSERT INTO example (non_null_val, null_val) VALUES (?, ?)");
      $non_null_val = 'ABC';
      $null_val = null;
      $stmt->bind_param('ss', $non_null_val, $null_val);
      $stmt->execute();
    
      $non_null_val = 'DEF';
      $null_val = null;
      $stmt->bind_param('ss', $non_null_val, $mysqli->real_escape_string($null_val));
      $stmt->execute();
    
      $stmt->close();
    
      $mysqli->close();
    ?>
    

    提供您需要的内容。

    绑定值时,您根本不应该使用
    real\u escape\u string

    这使得您最初的问题不是一个真实的问题-如果您确实将空值传递给此函数,而没有任何中间操作,则根本不会有任何问题

    编辑后的版本也不合法,因为它要求检查代码、查找错误、提出问题并回答问题。前两个动作你必须自己完成

    因此,要回答您的问题,只需从中获取代码:

    $password = null;
    $stmt->bind_param('ssssss', $email, $password, $fullname,
             $prof_image_url, $splash_image_url, $facebook_id);
    

    它可以绑定空值。

    当绑定值时,您根本不应该使用
    real\u escape\u string

    这使得您最初的问题不是一个真实的问题-如果您确实将空值传递给此函数,而没有任何中间操作,则根本不会有任何问题

    编辑后的版本也不合法,因为它要求检查代码、查找错误、提出问题并回答问题。前两个动作你必须自己完成

    因此,要回答您的问题,只需从中获取代码:

    $password = null;
    $stmt->bind_param('ssssss', $email, $password, $fullname,
             $prof_image_url, $splash_image_url, $facebook_id);
    

    它将绑定空值。

    显示空列而不是空列是完全正常的,即使它是…@chility,即使我使用“Select*from users where password is null”,它也不会显示任何具有空密码列的行。您需要显示更多代码,尽管--DB模式将与执行查询的实际代码一起提供帮助。显示空列而不是空列是完全正常的,即使它是…@chility,即使我使用“Select*from users where password is null”,它也不会显示任何具有空密码列的行。您需要显示更多代码,不过——DB模式将与执行查询的实际代码一起提供帮助。在将值与参数绑定时,永远不要使用
    real\u escape\u string
    。这个答案有多个问题!在将值与参数绑定时,永远不要使用
    real\u escape\u string
    。这个答案有多个问题!