php - rowCount 返回 0,即使它不应该返回

标签 php mysql pdo

$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

数据库中的unamevarchar(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/

相关文章:

php - 我应该如何设置我的类(class)?

php - MySQL 和 PHP : Sorting text by alphabetical order for English & string length for non-English

MySql 索引排序和位置

php - PDO::fetchAll 不适用于 MySQL 存储过程

php - 如果密码包含空格,PDO 无法连接

php - 安全高效的文件 uploader

php - 在上一个版本中如何将 Jquery-File-Upload PHP 与数据库一起使用?

php - 从 PHP 调用 Oracle 包中存在的函数/过程

php - MySQL 将两个查询组合成 JSON 对象

javascript - 在 React nodeJS 中使用 mysql