print_r return 1 虽然没有记录被保存
print_r return 1 althought no record was saved
我有一些方法可以像这样保存包括用户令牌和电子邮件在内的记录:
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();
}
}
当我使用 print_r $stmt->rowCount() 时它 return '1',但是当检查我的 reset_token 时我没有发现任何数据。那么我的代码有什么问题吗?
经过一些调查,我意识到你在问为什么这个函数是 returning 1
而你期望 true
。当你的函数 return 为真时,它实际上 return 为 1。所以它会 return 1
但如果你这样做 if($user->saveResetToken($email, $activation)) print('this is true')
你可以看到这实际上是评估是的。
试试这个:
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
在php中true
实际上表示不为零。
以下是文档中被视为错误的值:
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
查看 php docs on booleans 了解更多信息。
至于未保存的记录,您必须在我的评论中提到的查询中使用 ->execute()
。
我有一些方法可以像这样保存包括用户令牌和电子邮件在内的记录:
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();
}
}
当我使用 print_r $stmt->rowCount() 时它 return '1',但是当检查我的 reset_token 时我没有发现任何数据。那么我的代码有什么问题吗?
经过一些调查,我意识到你在问为什么这个函数是 returning 1
而你期望 true
。当你的函数 return 为真时,它实际上 return 为 1。所以它会 return 1
但如果你这样做 if($user->saveResetToken($email, $activation)) print('this is true')
你可以看到这实际上是评估是的。
试试这个:
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
在php中true
实际上表示不为零。
以下是文档中被视为错误的值:
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
查看 php docs on booleans 了解更多信息。
至于未保存的记录,您必须在我的评论中提到的查询中使用 ->execute()
。