当你prepare()
一个声明(在我的情况下是一个更新声明)然后execute()
时,有人能告诉我,然后在更新查询后使用rowCount()
检查(参见下面的代码) )如果rowCount() > 0
如果是,则找到匹配并进行更新,但我得到了一个else语句。
为了确保我不会在语句中混淆语法错误和条件,我想在下面的代码中询问(详细说明具体区域)else语句是否基本上意味着无法更新,因为匹配是未找到和/或可能的语法错误或其他一些错误?我认为这意味着我大胆地想要确保我不会让自己感到困惑。
请忽略prepare语句中的SQL UPDATE语法本身,因为它错了,稍后会处理。我认为代码更好地解释了我正在谈论的领域中的详细评论。
// check if key is set and alphanumeric and equals 40 chars long
// we use sha1 so it will always be 40 chars long.
if(isset($_GET['key']) && ctype_alnum($_GET['key']) && strlen($_GET['key']) == 40){
$key = trim($_GET['key']);
}
// if key isset and valid
if(isset($key)){
try {
// connect to database
$dbh = sql_con();
// checke if activation key matches and user_uid matches
$stmt = $dbh->prepare("
SELECT
users_status.user_uid,
users_status.user_activation_key
FROM
users_status
JOIN
users
ON
users_status.user_activation_key = ?
AND
users_status.user_uid = users.user_uid LIMIT 1");
// execute query
$stmt->execute(array($key));
// if row count greater than 0 then match found
if ( $stmt->rowCount() > 0 ) {
// user verified; we now must update users status in users table to active = 1
// and set the user_activation_key in the users_status to NULL
$stmt = $dbh->prepare("
UPDATE
users.user_status,
users_status.user_activation_key
SET
user_status = ".USER_STATUS_ACTIVE.",
user_activation_key = NULL
JOIN
users
ON
users_status.user_activation_key = ?
AND
users_status.user_uid = users.user_uid LIMIT 1");
// execute query
$stmt->execute(array($key));
if ( $stmt->rowCount() > 0 ) {
echo 'account now activated';
exit;
} else {
// update not sucessful
// THIS IS THE BIT IM CONFUSED WITH;
// IF RETURNED RESULT IS 0 (WHICH IT WILL BE IF I GET HERE WHEN RUNNING SCRIPT)
// THEN I GUESS THAT MEANS THERE WAS NOT AN ERROR IN SQL SYNTAX BUT
// CONDITION IN SQL STATEMENT COULD NOT BE MATCHED ? IS THAT CORRECT WHAT I AM THINKING ?
// IF I AM CORRECT THEN OBVIOUSLY I WILL DISPLAY A MESSAGE TO USER AND EXIT HERE;
// AS IF I AM THINKING RITE ANY SYNTAX ERROR WOULD BE CAUGHT BY CATCH BLOCK AND THIS ELSE STATEMENT
// MEANS COULD NOT UPDATE BECAUSE NO MATCH IN UPDATE QUERY COULD BE FOUND ?
}
} // else no match found
else {
// no match found invalid key
echo '<h1>Invalid Activation Link</h1>';
$SiteErrorMessages =
"Oops! Your account could not be activated. Please recheck the link in your email.
The activation link could not be found or the account has already been activated.";
SiteErrorMessages();
include($footer_inc);
exit;
}
// close database connection
$dbh = null;
} // if any errors found log them and display friendly message
catch (PDOException $e) {
ExceptionErrorHandler($e);
require_once($footer_inc);
exit;
}
} else {
// else key not valid or set
echo '<h1>Invalid Activation Link</h1>';
$SiteErrorMessages =
"Oops! Your account could not be activated. Please recheck the link in your email.
The activation link appears to be invalid.<br /><br />
If the problem persists please request a new one <a href='/member/resend-activation-email'>here</a>.";
SiteErrorMessages();
include($footer_inc);
exit;
}
答案 0 :(得分:1)
你是对的:
if $stmt->rowCount() == 0
然后就意味着没有更新任何行。
如果您在执行查询时遇到sql错误或错误,则在执行时会收到FALSE返回值或PDO EXCEPTION
execute(array($key));
答案 1 :(得分:1)
如果您正在进行多次更新,则应该真正使用交易。
请注意,默认的MyISAM引擎不支持事务,因此如果您希望这样做,则需要ALTER TABLE tbl_name ENGINE=InnoDB
:
$success = false;
$dbh->beginTransaction();
# perform your first query
if ($query->rowCount() == 1) {
# something was updated/inserted/deleted
# perform second query
if ($query->rowCount() == 1) {
$success = true;
}
}
if ($success) $dbh->commit();
else $dbh->rollBack();
至于你的问题,你可能需要包围你的?使用单引号,所以将您的陈述更改为:
users_status.user_activation_key = '?';
您可能无法获得结果的另一个原因是,如果您的$ key是一个整数并且您使用PreparedStatement::execute($array)
方法绑定参数,则需要确保将值转换为适当的类型它起作用,例如:
$query->execute(array((int)$key));
否则只需使用$query->bindParam($key)