$this->db->beginTransaction();
$this->db->query ('LOCK TABLES users WRITE');
$sql = 'INSERT INTO users (uname) VALUES (:uname)';
$sth = $this->db->prepare ($sql);
$sth->bindParam (':uname', $uname);
$sth->execute ();
if ($sth->rowCount()==0) {
$this->db->rollBack();
$this->db->query ('UNLOCK TABLES');
throw new Exception('<strong>Oh snap!</strong> User name is taken! Try again.');
}
我在我的数据库中手动设置了一个名为“test”的用户。当我创建一个名为“test2”的用户时,它起作用了。但是每当我尝试创建第三个用户时,我都会得到 rowCount = 0
。
数据库中的uname
是varchar(15)
。
if(isset($_POST['regUser']) && isset($_POST['regPwd']) && isset($_POST['regConfirmPwd'])) {
if($_POST['regPwd'] == $_POST['regConfirmPwd'] ) {
$user->newUser($_POST['regUser'], $_POST['regPwd']);
} else {
$user->error = "<strong>Oh snap!</strong> The passwords don't match!";
}
}
将发布信息发送到我的 newuser 函数,它会在第一位代码处停止。有什么想法吗?
最佳答案
与其插入用户然后在获取后回滚,不如先测试该用户是否可用(在锁定表之后),然后放心地进行插入。
关于php - rowCount 返回 0,即使它不应该返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15113356/