Php 如何确保删除查询删除一行?
这是我的密码:Php 如何确保删除查询删除一行?,php,mysql,sql,pdo,Php,Mysql,Sql,Pdo,这是我的密码: $token = $_GET['validate_activate']; // it's something like this "98j34f0j043f094f98325r" $user_id = $_GET['user_id']; // db connection here $stmt = $dbh->prepare("DELETE FROM activate_token WHERE token = :token"); $stmt->execute(arr
$token = $_GET['validate_activate']; // it's something like this "98j34f0j043f094f98325r"
$user_id = $_GET['user_id'];
// db connection here
$stmt = $dbh->prepare("DELETE FROM activate_token WHERE token = :token");
$stmt->execute(array(':token' => $token));
if ( /* deleting is success */ ) {
// activating the user
} else {
// your token is invalid
}
那么,我如何才能为if
语句建立正确的条件呢?借助于
PDOStatement::rowCount()返回受
由相应的
PDO语句对象
借助于
PDOStatement::rowCount()返回受
由相应的
PDO语句对象
这应该是可行的,如果delete查询成功,pdo语句应该返回True或False
if($stmt->execute(array($token = $_GET['validate_activate']))) {
// activating the user
} else {
// your token is invalid
}
这应该是可行的,如果delete查询成功,pdo语句应该返回True或False
if($stmt->execute(array($token = $_GET['validate_activate']))) {
// activating the user
} else {
// your token is invalid
}
$stmt->execute
返回bool结果,如果sql查询成功,则返回true,因此只需在if语句中使用该结果:
$stmtWorked = $stmt->execute(array($token));
if ($stmtWorked) {
// activating the user
} else {
// your token is invalid
}
另外,如果需要知道具体数量,您还可以使用
mysqli\u stmt\u infected\u rows()
统计删除的许多行。$stmt->execute
返回bool结果,如果sql查询成功,则返回true,因此只需在if语句中使用该结果:
$stmtWorked = $stmt->execute(array($token));
if ($stmtWorked) {
// activating the user
} else {
// your token is invalid
}
此外,如果您需要知道具体的删除量,您还可以使用
mysqli\u stmt\u infected\u rows()
统计删除的行数。hello@Drew更新版本中是否存在错误?已经有两年没有使用PHP了。如果它能在您的部署中工作,并且经过良好测试,那么您的方法就是我要使用的方法。只是当你推送你的代码时,它取决于实际的设置。来自手册:hello@Drew更新版本中有bug吗?已经有两年没有使用PHP了。如果它能在您的部署中工作,并且经过良好测试,那么您的方法就是我要使用的方法。只是当你推送你的代码时,它取决于实际的设置。从手册中:或使用select count
+fetchColumn
或使用select count
+fetchColumn
可以成功,并且不会删除行。假设id为无符号
(0或更大),从id=-1的表中删除
将返回true
它可以成功并且不删除行。假设id为无符号
(0或更大),从id=-1的表中删除
将返回true