dongzhuonao8429 2015-03-25 02:49
浏览 28
已采纳

print_r返回1但是没有保存记录

I have some method to save record including user token and email like this:

public function saveResetToken($email, $resetToken)
    {
        try {

            $conn = Database::getConnection();
            // Connect and create the PDO object
            $conn->exec('SET CHARACTER SET utf8');      // Sets encoding UTF-8
            // Define and prepare an INSERT statement
            $sql = 'UPDATE users SET reset_token=:token WHERE email =:email limit 1';
            $stmt =$conn->prepare($sql);

            // Adds value with bindParam
            $stmt->bindParam(':email', $email, PDO::PARAM_STR);
            $stmt->bindParam(':token', $resetToken,PDO::PARAM_STR );

            if ($stmt->rowCount()===1){   

                $conn = null;        // Disconnect
                return true;

            }else{
                return false;
            }

        } catch (PDOException $e) {
            include('../views/error.php');
            include('../views/admin/includes/footer.php');
            exit();
        }
    }

when i use print_r $stmt->rowCount() it return '1', but when check at my reset_token i find no data. so what wrong in my code?

  • 写回答

1条回答 默认 最新

  • douguazhi5966 2015-03-25 03:34
    关注

    Upon some investigation I realized you are asking why this function is returning 1 when you are expecting true. When your function returns true it actually returns a 1. So it will return 1 but if you do if($user->saveResetToken($email, $activation)) print('this is true') you can see this is actually evaluating to true.

    Try this:

    function myFunc() {
      return true;
    }
    echo myFunc(); // output: 1
    echo myFunc()==true; // output: 1
    echo myFunc()==1; // output: 1
    echo myFunc()===true; // output: 1
    echo myFunc()===1; // output: 1
    

    In php true actually means not zero.

    Here are the values that are considered false from the docs:

    When converting to boolean, the following values are considered FALSE:

    the boolean FALSE itself the integer 0 (zero) the float 0.0 (zero) the empty string, and the string "0" an array with zero elements an object with zero member variables (PHP 4 only) the special type NULL (including unset variables) SimpleXML objects created from empty tags

    Check out the php docs on booleans for more information.

    As for the record not being saved you must use the ->execute() on the query as mentioned in my comment.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。