mysqli_stmt :: execute()返回false而语句未执行

时间:2013-08-14 18:57:21

标签: php mysql sql mysqli

我有一个简单的登录系统保护机制,通过记录用户的IP,失败的尝试次数和上次尝试时间到名为bannedusers的MySQL数据库表。但是,当我尝试使用下面的代码将新条目插入数据库时​​,execute()函数返回false并且无法执行。

代码如下:

private $con;

function updateTable($IP, $attempt, $exists){
    $time = time();

    //The following statement is actually in the constructor, moved here for completeness
    $this->con = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME); //All these constants are predefined and verified to be correct.

    if($this->con->connect_error){
        return true; //If there is a connection error, just let the user log in... We'll deal with this later
    }

    //Another function already determines if the entry exists or not.
    if(!$exists){
        //ip, retrycount, attempttime are the name of the fields. IP is a 40-char wide VARHCAR, retrycount is a tinyint and attempttime is a big int.
        $query = "INSERT INTO bannedusers (ip, retrycount, attempttime) VALUES (?,?,?)";

        if($stmt = $this->con->prepare($query)){

            //This following statement executes without throwing errors and returns true.
            $stmt->bind_param('sii', $IP, $attempt, $time);

            $successful = $stmt->execute();
            $stmt->close();

            if(!$successful){
                //Causes a small dialog to appear telling you the query failed.
                echo "<script type='text/javascript'>alert('Failed query!');</script>";
            }
        }
    }else{
        //Unrelated code omitted.
    }
}

我是php和MySQL的新手,通过研究我发现SQL语法显然需要在查询的VALUE部分的字段周围使用引号,如:

$query = "INSERT INTO bannedusers (ip, retrycount, attempttime) VALUES ('?','?','?')";

但我发现here它实际上阻止了查询的工作(仍然尝试了它并在bind_param()上出错)。我已经尝试将类型更改为'sii'或'sss'或'ssi',这导致查询失败。我已经尝试在SQL查询结束时添加分号,但没有改变任何内容。在所有情况下,“查询失败!”弹出对话框时没有其他错误(除了上面提到的那个,我在VALUES字段周围使用了引号。

感谢任何帮助。

更新

事实证明$ip在传递给函数之前不知何故是null,如果将MySQL错误级别提升到MYSQLI_REPORT_ALL,则只会导致错误。

1 个答案:

答案 0 :(得分:4)

将mysqli设置为Exception模式

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

或者总是检查每个mysqli操作的结果并手动抛出mysqli错误:

$result = $stmt->execute();
if (!$result) {
    throw new Exception($mysqli->error);
}

这是唯一方式来了解您的execute();

  

我发现SQL语法显然需要VALUE字段周围的引号

当然是错的。 SQL语法显然只需要围绕字符串的引号。