获取行和的SQL语句错误

时间:2016-06-19 12:53:00

标签: php sql

我有一个像彩票一样的游戏。通常当用户注册代码时,该代码是唯一的,并且数据库应该检查该代码是否存在以及是否存在应该更新注册用户并获取消息"您的代码已注册" ...每个代码都可以只能注册一次。但我收到一个错误,它说我一直试着注册一个代码,而你的代码不存在,但代码在银行。

function checkCode($db,$code){
    $stmt = $db->prepare("INSERT INTO code (code) VALUES (:code)");
    $stmt->bindParam(':code', $codex);
    $codex = $code;
    $stmt->execute();
    //enter code here
    ///// until here is ok
    $sql = "SELECT * FROM code"; 
    $result = $db->prepare($sql); 
    $result->execute();
    ////here is what i think generate the error
    $number_of_rows = $result->rowCount();
    if($number_of_rows >0){
        return true;
    } else{
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

http://php.net/manual/en/pdostatement.rowcount.php

rowCount获取受影响的行数。 SELECT语句不会影响任何行。您应该检查rowCount的{​​{1}},而不是$stmt