Mysqli准备声明 - 返回错误,但为什么?

[英]Mysqli Prepare Statement - Returning False, but Why?

I have a function that generates a prepared INSERT statement based on an associative array of column names and values to be inserted into that column and a table name (a simple string):


function insert ($param, $table) {
        $sqlString = "INSERT INTO $table (".implode(', ',array_keys($param)).') VALUES ('.str_repeat('?, ', (count($param) - 1)).'?)';
        if ($statement = $this->conn->prepare($sqlString)):
            $parameters = array_merge(array($this->bindParams($param), $param));
            call_user_func_array(array($statement, 'bind_param', $parameters));
            if (!$statement->execute()):
                die('Error! '.$statement->error());
            return true;
            die("Could Not Run Statement");

My problem is that $this->conn->prepare (it's part of a class, conn is a NEW mysqli object, which works with no issues) returns false, but does not give me a reason why!

我的问题是$ this-> conn-> prepare(它是一个类的一部分,conn是一个新的mysqli对象,它没有问题)返回false,但是没有给我一个理由!

Here is a sample $sqlString that gets built for the prepare statement:

以下是为prepare语句构建的示例$ sqlString:

INSERT INTO students (PhoneNumber, FirstName, MiddleInit, LastName, Email, Password, SignupType, Active, SignupDate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)

Can anyone see a problem with this parameterized statement? Any reason the prepare function would return false?

任何人都可以看到这个参数化语句的问题? prepare函数返回false的原因是什么?

1 个解决方案



I'm copying the solution into this answer so this can be given an upvote, otherwise the question will appear in the "unanswered questions" forever. I'm marking this answer CW so I won't get any points.


@Andrew E. says:

@Andrew E.说:

I just turned on mysqli_report(MYSQLI_REPORT_ALL) to get a better understanding of what was going on - turns out that one of my field names was incorrect - you'd think that prepare() would throw an exception, but it fails silently.

我只是打开mysqli_report(MYSQLI_REPORT_ALL)来更好地理解发生了什么 - 事实证明我的一个字段名称是不正确的 - 你认为prepare()会抛出异常,但它会无声地失败。



  © 2014-2022 联系我们: